简体   繁体   English

如何使用data-attribute设置Select HTML tag的选项

[英]How to set Option of Select HTML tag using data- attribute

How to set Option of Select HTML tag using data- attribute? 如何使用data-attribute设置Select HTML tag的选项?

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 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.
});

Set the select value to the value of the matching option : select值设置为匹配option的值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用html“data-”属性 - Using an html “data-” attribute 更改选项时如何从表中动态创建的选择中获取 data-* 属性的值 - How to get the value of an data-* attribute from a dynamically created select in a table when I change an option 创建 select2 输入时如何为每个选项添加 data-* 属性? - How to add a data-* attribute to each option when creating a select2 input? 我想使用jQuery在html中使用data- *属性将占位符设置为文本框? - I want to set placeholder to textbox using data-* attribute in html using jQuery? 使用自定义“数据-”属性而不是ID选择对象 - select object using custom “data-” attribute instead of id 添加属性以选择使用父级的数据属性? - adding attribute to select using parent's data- attribue? javascript使用data-属性和值将焦点设置在contenteditable区域上 - javascript set focus on contenteditable area using data- attribute and value 使用JS访问HTML数据属性,而无需使用.getelementbyid? - Access HTML data- attribute with JS, WITHOUT using .getelementbyid? 获取选定对象的 data-* 属性<option value=""></option> - Get data-* attribute of the selected <option> 如何使用jquery将选中的收音机的data-*属性设置为另一个元素的类? - How to set data-* attribute of checked radio to class of another element using jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM