jQuery中的条件语句

时间:2016-11-18 01:12:11

标签: javascript jquery ruby-on-rails ruby

我在下面有jQuery代码,目前它有一个Min RentMax Rent字段。即使在$val1 to $0字段中未输入值,输入值时也会显示Max Rent。我想要完成的是,如果在两个字段中输入值,则表示$val1 to $val2。否则我只想让它显示一个值。我尝试用条件语句来做这个,但我的jQuery不是很强大,不知道我哪里出错。

jQuery的:

$("[data-toggle='popover']").on('shown.bs.popover', function() {
  $("#listing-price-selector").next().find("#listing_search_form_min_price").keyup(modPrice);
  $("#listing-price-selector").next().find("#listing_search_form_max_price").keyup(modPrice);
});

$("[data-toggle='popover']").on('hide.bs.popover', function() {
modPrice();
});
modPrice();

function modPrice() {
  if ($("#listing-price-selector") && $("#listing-price-selector").next()) {
    var mn = $("#listing-price-selector").next().find("#listing_search_form_min_price").val();
    var mx = $("#listing-price-selector").next().find("#listing_search_form_max_price").val();
    if (mn || mx) {
      mn = (mn == "") ? 0 : mn;
      mx = (mx == "") ? 0 : mx;
      $("#priceBox").val(mn + " to " + mx);
      if ($("#listing_search_form_max_price").length > 0) {
        $("#listing-price-selector").text(" $" + mn + " to $" + mx);
      else
        $("#listing-price-selector").text(" $" + mn);
      }
    }
  }
}

HTML:

<div class="col-md-2 col-xs-6">
    <a tabindex="0" class="button btn-transparent" id="listing-price-selector" role="button" data-toggle="popover">Price Range <span class="caret"></span></a>
</div>

<div id="listing-price-content" style="display: none;">
  <div class="container-fluid">
    <div class="row">
      <div class="col-xs-6">
        <div class="input-group input-group-sm">
          <span class="input-group-addon" id="basic-addon1">$</span>
          <%= f.text_field :min_price, class: "form-control", placeholder: "Min Rent", data: { "binding-name" => "min-price" } %>
        </div>
      </div>
      <div class="col-xs-6">
        <div class="input-group input-group-sm">
          <span class="input-group-addon" id="basic-addon1">$</span>
          <%= f.text_field :max_price, class: "form-control", placeholder: "Max Rent", data: { "binding-name" => "max-price" } %>
        </div>
      </div>
    </div>
  </div>

1 个答案:

答案 0 :(得分:1)

我不确定我的评论是否有帮助。所以我发布这个。 如果这是您的问题所在,则不建议在此处进行此类检查。如你所知,他们可以输入&#34; abcd&#34;

if ($("#listing_search_form_max_price").length > 0) {
    $("#listing-price-selector").text(" $" + mn + " to $" + mx);
  else
    $("#listing-price-selector").text(" $" + mn);
  }

你可以试试这个

function modPrice() {
    if ($("#listing-price-selector") && $("#listing-price-selector").next()) {
        var mn = $("#listing-price-selector").next().find("#listing_search_form_min_price").val();
        var mx = $("#listing-price-selector").next().find("#listing_search_form_max_price").val();
        if (mn || mx) {
            mn = (mn == "") ? 0 : mn;
            mx = (mx == "") ? 0 : mx;
            $("#priceBox").val(mn + " to " + mx);
            if (parseFloat($("#listing_search_form_max_price").val()) > 0) {
                $("#listing-price-selector").text(" $" + mn + " to $" + mx);
            } else {
                $("#listing-price-selector").text(" $" + mn);
            } //END listing_search_form_max_price
        } //END mn || mx
    } //END listing-price-selector
}

告诉我们。