2

ドロップダウン内の値(バランス)を比較し、これをグローバル変数と比較してから、if/elseステートメントを実行してHTMLを表示する必要があるドロップダウンがあります。

ドロップダウンは次のとおりです。

 <select name="batch">
 <option value='null'>-- None --</option>
 <option value='96'>Check (#2200) (Bal. $84.00) - Jim Jones</option>
 <option value='98'>Credit Card (#0) (Bal. $90.00) - Bailey Adams</option>
 </select>

これが私のjqueryです:

    $("select[name=batch]").change(function () {
    if ($(this).val() >= GlobalTotal) {
        $("th#show_refund").show();
        $("th#no_show_refund").hide();
    } else {
        $("th#show_refund").hide();
        $("th#no_show_refund").show();
    }
});

jqueryがHTMLselect内のバランスを確認することは可能ですか?もしそうなら、私は正しい方向に進むための指針に感謝します。

4

4 に答える 4

2

これを動的に構築していると思います。各オプションで、属性data-balance=84またはバランスを追加してから、jQueryを使用して、次を使用します

$("select[name=batch] option:selected").attr("data-balance")

したがって、完全なコードは次のようになります。

<select name="batch">
    <option value='null'>-- None --</option>
    <option value='96' data-balance="84.00">Check (#2200) (Bal. $84.00) - Jim Jones</option>
    <option value='98' data-balance="90.00">Credit Card (#0) (Bal. $90.00) - Bailey Adams</option>
</select>

$("select[name=batch]").change(function () {
    if ($("select[name=batch] option:selected").attr("data-balance") >= GlobalTotal) {
        $("th#show_refund").show();
        $("th#no_show_refund").hide();
    } else {
        $("th#show_refund").hide();
        $("th#no_show_refund").show();
    }
});
于 2012-05-10T19:20:17.070 に答える
2

innerHTML選択した値からその値を取得できます。

// Store our Global Total
var gTotal = 87;
// Bind to the change event of our dropdown
$("select[name='batch']").on("change", function(){
  // If we find a dollar amount in our selected element, save it to `amount`
  if ( amount = $(":selected", this).html().match( /\$(\d+\.\d{2})/ ) )
    // If that amount is greater than the Global Total
    amount[1] >= gTotal
      // Do something when it's greater than
      ? console.log( amount[1] + ' is greater than ' + gTotal )
      // Do something else when it's lesser than
      : console.log( amount[1] + ' is less than ' + gTotal ) ;
});

デモ: http://jsbin.com/upotuy/2/edit

于 2012-05-10T19:21:25.163 に答える
0

次のように、選択オプションの値を残高に設定することをお勧めします。

<select name="batch">
    <option value='null'>-- None --</option>
    <option value='84.00'>Check (#2200) (Bal. $84.00) - Jim Jones</option>
    <option value='90.00'>Credit Card (#0) (Bal. $90.00) - Bailey Adams</option>
</select>

これにより、ファンキーなテキスト解析を必要とせずに値を取得できます。例えば:

$("select[name=batch]").change(function () {
    if ($("select[name=batch] option:selected").val() >= GlobalTotal) {
        $("th#show_refund").show();
        $("th#no_show_refund").hide();
    } else {
        $("th#show_refund").hide();
        $("th#no_show_refund").show();
}
于 2012-05-10T19:17:05.760 に答える
0
$("select[name=batch]").change(function () {
 var selectedText= "";
      $("select option:selected").each(function () {
            selectedText+= $(this).text() + " ";
          });
于 2012-05-10T19:30:46.657 に答える