如何使用数据属性设置选择HTML标记的选项?
HTML
<select class="form-control shade_custom_option valid" id="1">
<option data-isnone="true" selected="selected" value="1">None</option>
<option data-isnone="false" value="2">Text1</option>
<option data-isnone="false" value="3">Text2</option>
</select>
<select class="form-control shade_custom_option valid" id="2">
<option data-isnone="true" selected="selected" value="1">None</option>
<option data-isnone="false" value="2">Text1</option>
<option data-isnone="false" value="3">Text2</option>
</select>
JS
$(".shade_custom_option").each(function (index, value) {
// var optionSelected = $("option:selected", value);
// How to get option with data-isnone="true"? and set it as selected option.
});
答案 0 :(得分:2)
将select
值设置为匹配的option
:
$(".shade_custom_option").each(function (index, value) {
$(this).val( $(this).find('option[data-isnone=true]').val() );
});