简体   繁体   English

jQuery:设置 <option>根据检查<select>属性

[英]Jquery: set <option> checked based on <select> attribute

I'm trying to write a simple jquery script that would loop through all <select> elements on the page and pick up an attribute named selected and set option value to selected based on that. 我正在尝试编写一个简单的jquery脚本,该脚本将遍历页面上的所有 <select>元素,并选择一个名为selected的属性,并根据该属性将option值设置为selected。

example: 例:

<select selected="2">
  <option value="1">One</option>  
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

On the example above the code would set option "two" to selected. 在上面的示例中,代码会将选项“两个”设置为选中。

What is the cleanest way to write this? 什么是写这最干净的方法?

edit: 编辑:

For clarification, the main problem with my question and the example above is that I was using reserved keyword 'selected'. 为了澄清起见,我的问题和上面的示例的主要问题是我使用的是保留关键字“ selected”。 I would suggest using another attribute name (I changed my to data-selected, this is even valid with html5-spec :-) ). 我建议使用另一个属性名称(我将其更改为data-selected,这甚至对于html5-spec:-都有效)。

<select data-selected="2">
  <option value="1">One</option>  
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

selected is a reserved property. selected是保留的属性。 No matter what you set it to, jQuery will return its value as "selected." 无论您将其设置为什么,jQuery都将其值返回为“ selected”。

Instead, you can access the DOM attributes collection directly, like this: 相反,您可以直接访问DOM attributes集合,如下所示:

 $('select[selected]').val(function() { return this.attributes.selected.value; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select selected="1"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> <select selected="2"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> <select selected="3"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> 

options have selected attributes. optionsselected属性。 You can't have a selected attribute on a select element like that. 这样的select元素上不能具有selected属性。 You can have a data-attribute, or a name instead perhaps. 您可以具有数据属性,也可以具有名称。

$('select[data-selected="1"]')

or 要么

$('select[name="1"]')

To get the option with a value of 1 you can simply do and add a selected attribute to it: 要获得值为1的选项,您可以简单地添加一个选定的属性:

$('select[data-selected="1"] option[value="1"]').attr('selected', 'selected');

DEMO DEMO

As mentioned in the other answers, selected is a reserved keyword. 如其他答案所述, selected是保留关键字。

Tweaked your html and this is now working: 调整了html,现在可以正常工作了:

<select mysv="2">
  <option value="1">One</option>  
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

$("select").each(function(){
    var sv = $(this).attr("mysv");
  $(this).find("option[value="+sv+"]").attr("selected", "selected");
});

This is the code I finally ended with. 这是我最终结束的代码。 It has one extra thing that other solutions didn't have, checking of attribute availability. 它具有其他解决方案所没有的另一件事,即检查属性可用性。 Other solutions will set the select field blank (even if there are no blank options available) if the attribute is missing. 如果缺少属性,其他解决方案会将选择字段设置为空白(即使没有空白选项可用)。

$("select").each(function() {
   var selected_val = $(this).attr("data-selected");
   if(selected_val)
      $(this).val(selected_val);
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM