如何使用data-属性设置选择HTML标记的选项

时间:2014-09-30 13:18:54

标签: javascript jquery html html-select

如何使用数据属性设置选择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.
});

1 个答案:

答案 0 :(得分:2)

select值设置为匹配的option

的值
$(".shade_custom_option").each(function (index, value) {
  $(this).val( $(this).find('option[data-isnone=true]').val() );
});