0
私はなMultipleChoiceFieldと変数my_choice = 'mystring'
テキスト変数に基づいて、多肢選択法オプションを選択する方法
私はここでmy_choice
私html
にオプションの相対チェックしたい持っている:
ul id="id_layer_select"><li><label for="id_layer_select_0"><input checked="checked" id="id_layer_select_0" name="layer_select" type="checkbox" value="1" /> <span style='font-weight:normal'>Mychoice1</span></label></li>
<li><label for="id_layer_select_1"><input id="id_layer_select_1" name="layer_select" type="checkbox" value="2" /> <span style='font-weight:normal'>Mychoice2</span></label></li>
<li><label for="id_layer_select_2"><input id="id_layer_select_2" name="layer_select" type="checkbox" value="3" /> <span style='font-weight:normal'>Mychoice3</span></label></li> [...]</ul>
ここに私をjavascript
:
var my_choice = 'mystring'
var opt = $("#id_layer_select option");
opt.filter("[value=my_choice]").attr("selected", true);
jqueryを使用できます。オフコースmystring
私はid_layer_select_x
を使いたくないと私は追加して、属性ではないことを好むオプションMychoice1
またはMychoice2
かなど
です:
PSはありがとうございます。
問題は、チェックボックスのチェックを外し滞在し、
console.log(opt)=Object { length: 0, prevObject: Object[1] }
あなたは正しいですが、まだ動作しません。 Console.log(opt)=オブジェクト{length:0、prevObject:Object [1]}。それは私に間違って見える。 – fabio
アップデートをご覧ください –