我有下面的jQuery代碼,目前它有一個Min Rent
和Max Rent
字段。即使在Max Rent
字段中未輸入值,現在輸入值時也是$val1 to $0
。我試圖完成的是,如果在兩個字段中輸入一個值,表示$val1 to $val2
。否則,我只想讓它顯示一個值。我試圖用條件語句來做到這一點,但我的jQuery不是很強大,不知道我哪裏出錯了。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>
你的代碼真的說'if($(#listing_search_form_max_price).length> 0){'?那裏不應該有一些報價? –
@ muistooshort哎呀。那裏應該有引號,但那仍然不能解決問題。 –
嗨,你能指點我哪個'如果'在這裏造成問題?我仍然試圖解決這個問題..' $ val1 to $ 0' ..所以你可以得到'mn'和'mx'值,並且你試圖修復'if(mn || mx){' ? *否則,我只是希望它顯示一個值*在哪裏? – Searching