2016-11-18 8 views
1

私は以下のjQueryコードを持っており、現在はMin RentMax Rentフィールドを持っています。 Max Rentフィールドに値が入力されていなくても、値が入力された直後は$val1 to $0と表示されます。私が達成しようとしているのは、両方のフィールドに値が入力されて、$val1 to $val2と表示されている場合です。さもなければ、私はそれが1つの値を表示したいだけです。私は条件付きのステートメントでこれを実行しようとしましたが、私の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> 

+0

あなたのコードは本当に 'if($(#listing_search_form_max_price).length> 0){'?そこに引用符を入れてはいけませんか? –

+0

@muistooshortおっと。そこに引用符を入れる必要がありますが、それでも問題は解決しません。 –

+0

こんにちは、もしここで問題が起きているのであれば、私に指摘してください。私はまだ '$ val1〜$ 0'という質問を試してみたいと思います。あなたは' mn'と 'mx'の値を得ることができ、あなたは' if(mn || mx){' ? *そうでなければ、ただ一つの値を表示したい*どこですか? – Searching

答えて

1

私は私のコメントが助けたかどうかわかりませんでした。だから私はこれを投稿している。 これがあなたの問題の場所ですが、ここでのこのタイプのチェックはお勧めできません。入力 "ABCD"

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 
} 

が私たちを知ってみましょうしようとすることができ、すべてのためにあなたが知っている彼らがすることができます。

+0

それは私の価格帯のドロップダウンを破った。 –

+0

私はそのif文が存在するところで道が離れるかもしれないことを覚えておいてください... –

+0

うーん..しかし、これはまだそこにあったものの98%です。ここで唯一の変更はif節です.. – Searching