2011-10-24 5 views
0

:containsここで私にとってはそれをしていません。それはあまりにも粗い。私はそれを得る正しい方法を理解しているようです。JQueryドロップダウン・オプションから必要なテキストを正確に選択するにはどうすればよいですか?

(以下の.jsフィドル)

$(function() { 
    $('#my_button').click(function() { 
     var cookieVal = "ab"; //This is what I want to find 
     var select = "combo0"; 

     var optionThatMatchesCookie = $("#" + select + " option:contains('" + cookieVal + "')").val(); 
     alert(optionThatMatchesCookie); //returns the wrong entry! (returns 'cabd' not 'ab') 

     if (typeof(optionThatMatchesCookie) != "undefined") { 
      //now put that in the combobox 
      $('#' + select).val(optionThatMatchesCookie); 
     } 
    }); 
}); 

<select class="inputfield" id="combo0" name="jobType"> 
    <option selected="selected">-select-</option> 
    <option id="1">sssssssssss</option> 
    <option id="18">fffffffffff</option> 
    <option id="47">cabd</option> 
    <option id="3">LPN/LVN</option> 
    <option id="22">bbbbb</option> 
    <option id="17">hhhhhhhhhh</option> 
    <option id="15">aaaaaaa</option> 
    <option id="16">zzzzzzzzzzzzzzz</option> 
    <option id="5">ab</option> 
    <option id="44">YYYYYYYYY</option> 
    <option id="19">XXXXXX</option> 
</select> 
<input type="button" id="my_button" value="test me"/> 

http://jsfiddle.net/KGabbert/mCDer/3/

答えて

9

それは "AB" が含まれているため、それはcabdオプションを返し、それは:containsがために使用されているまさにです。あなたが正確に一致optionの値を返すように希望している場合は、filterを使用することができます。

var optionThatMatchesCookie = $("#" + select + " option").filter(function() { 
    return $(this).val() === cookieVal; 
}).val(); 

ここworking exampleです。余談として

、あなたは括弧内のオペランドを置くべきではありませんのでtypeofは、オペレータ、ない方法であることに注意してください。

typeof optionThatMatchesCookie 

はまた、あなたがちょうどこれに大幅にあなたのコードを短くすることができることに注意してください。 cookieValoption要素の一つの値と一致しない場合

$(function() { 
    $('#my_button').click(function() { 
     var cookieVal = "ab", 
      select = "combo0"; 
     $("#" + select).val(cookieVal); 
    }); 
}); 

、値は変更されません。別のworking example hereを参照してください。

+0

'.text()'ではなく '.val()'のように見えます。 – Eric

+0

@Eric - どの部分について話していますか? '.text()'ではなく '.val()'を意味していたのですが、最初のコードスニペットの最後に '。 –

+0

@エリック - 申し訳ありませんが、ただ私の答えを読んで、あなたが意味するものを理解しました。私は自分自身を混乱させる。ありがとう! –

0

使用このコードはcookieValの一致を戻す:

var optionThatMatchesCookie = $('#' + select).children('option[text="' + cookie + '"]').val(); 

:ちょうどこれを見つけた

$(function() { 
    $('#my_button').click(function() { 
     var cookieVal = "ab"; //This is what I want to find 
     var select = $("#combo0"); //Select the combo here 

     var optionThatMatchesCookie = select.find("option").filter(function() { 
      return $(this).text() === cookieVal; 
     }).text(); 
     alert(optionThatMatchesCookie); //returns the wrong entry! (returns 'cabd' not 'ab') 

     if (typeof(optionThatMatchesCookie) != "undefined") { 
      //now put that in the combobox 
      select.val(optionThatMatchesCookie); 
     } 
    }); 
}); 
+0

この方法は機能しません:http://jsfiddle.net/KGabbert/8Te3B/ – KevinDeus

0

それはその場でセレクタを構築するためではない、通常、より良い練習ですそれは良い、これまたは。フィルタ()答え?

0

var optionThatMatchesCookie = $("#" + select + " value=['" + cookieVal +"']").val() 
関連する問題