0

OKなので、次のようなJSONを選択ボックスに入力しています。

Object{
"data":
[{"filter":"en","id":"2","label":"English"},
{"filter":"no","id":"3","label":"Norwegian"},
{"filter":"nl","id":"4","label":"Dutch"}]
}

現在jQuery("#mySelect").val()、選択したオプションの「id」の値を取得し、.text()または.html()を使用すると、選択したラベルの値を取得します。「filter」の値に基づいて選択したオプションを変更したいので、たとえば、選択したオプションに「no」のフィルターがあるかどうかを確認し、ない場合はオプションを選択します。します。

どんな助けでもいただければ幸いです。

4

2 に答える 2

2

目的にデータ属性を使用できます。

html

<select>
    <option data-filter="en" value="1">English</option>
    <option data-filter="no" value="2">Norwegian</option>
</select>​

js

$('select').change(function() {
    var id = $(this).find('option:selected').data('filter');
    alert(id);
});​

選択を動的に作成する場合は、次のようなデータ属性を設定できます。$('option').data('filter', 'no');

例: http://jsfiddle.net/kzHfF/

PS必要なだけ多くのデータ属性を配置できます。

于 2012-06-22T12:33:01.810 に答える
1

フィルタ値を含む各オプションに新しい属性を追加します

<select>
    <option filter="en" value="2">English</option>
    <option filter="no" value="3">Norwegian</option>
    <option filter="n1" value="4">Dutch</option>
</select>

$('select option:selected')を使用して選択したオプションを取得し、そこからフィルター属性の値を取得します。

$('select').change(function(){
    console.log($('select option:selected').attr('filter'));
});
于 2012-06-22T12:48:48.807 に答える