次のようにして、選択ボックスでオプションを選択できることを知っています
$('#selectBox :nth-child(4)').attr('selected', 'selected');
しかし、返された jQuery オブジェクトを介してこれを行うにはどうすればよいでしょうか? たとえば、次のようなもの
var box = $('#selectBox');
$(box :nth-child(4)').attr('selected', 'selected');
次のようにして、選択ボックスでオプションを選択できることを知っています
$('#selectBox :nth-child(4)').attr('selected', 'selected');
しかし、返された jQuery オブジェクトを介してこれを行うにはどうすればよいでしょうか? たとえば、次のようなもの
var box = $('#selectBox');
$(box :nth-child(4)').attr('selected', 'selected');
次のようなコンテキストでセレクターを使用できます。
$(':nth-child(4)', box).attr('selected', 'selected');
PS:.val()
オプションの値がわかっている場合は、メソッドを使用でき
$('#selectBox').val(the_value);
box.find(':nth-child(4)')[0].selected = true;