jQuery 无法通过 <option> 元素上的 attr() 设置“selected”=“selected”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3729741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Cannot set "selected"="selected" via attr() on <option> elements?
提问by user435216
<select>
<option>1</option>
<option>2</option>
<option class="test">3</option>
</select>
$('.test').attr('selected', 'selected');?
The select box above would choose the third option as default without any issues. However, if you check the elements with Firebug there isn't any selected="selected"
attribute present within the chosen <option>
.
上面的选择框会选择第三个选项作为默认选项,没有任何问题。但是,如果您使用 Firebug 检查元素,selected="selected"
则所选<option>
.
I know this isn't a big issue as it still worksbut I need selected="selected"
there so my other scripts could catch it and perform further processing. Are there any workarounds out there?
我知道这不是一个大问题,因为它仍然有效,但我需要selected="selected"
那里,以便我的其他脚本可以捕获它并执行进一步处理。有什么解决方法吗?
Thanks.
谢谢。
回答by bobince
The selected
attribute does notcorrespond to the current selectedness state of the option. The selected
attribute corresponds to the defaultselectedness, which will be restored if .reset()
is called (or a type="reset"
button is clicked) on the form. The current selectedness state can be accessed using the DOM property selected
, whereas the default-selectedness, as reflected by the attribute, is accessed under the DOM property defaultSelected
.
该selected
属性与选项的当前选择状态不对应。该selected
属性对应于默认选择,如果在表单上.reset()
调用(或type="reset"
单击按钮)将恢复该属性。当前选择状态可以使用 DOM 属性访问selected
,而默认选择状态(由属性反映)在 DOM 属性下访问defaultSelected
。
The same goes for value
vs defaultValue
on <input type="text">
/<textarea>
, and checked
/defaultChecked
on type="checkbox"
/"radio"
.
这同样适用于value
VSdefaultValue
的<input type="text">
/<textarea>
和checked
/defaultChecked
上type="checkbox"
/ "radio"
。
jQuery's attr()
is misleadingly named, and its attempts to pretend attributes and properties are the same thing is flawed. When you use attr()
, you are usually accessing the property, notthe attribute. Consequently, $(el).attr('selected', 'selected')
is actually doing el.selected= 'selected'
. This works because 'selected'
, as with any non-empty string, is ‘truthy': it gets converted to el.selected= true
. But this does not at any point touch the selected="selected"
attribute.
jQuery 的attr()
命名具有误导性,它试图假装属性和属性是同一个东西是有缺陷的。当您使用 时attr()
,您通常访问的是属性,而不是属性。因此,$(el).attr('selected', 'selected')
实际上是在做el.selected= 'selected'
。这是有效的'selected'
,因为 与任何非空字符串一样,是“真实的”:它被转换为el.selected= true
. 但这在任何时候都不会触及该selected="selected"
属性。
IE further complicates this already confusing situation by (a) getting getAttribute
/setAttribute
wrong so it accesses the properties instead of the attributes (which is why you should never use these methods in an HTML document), and (b) incorrectly mapping the current form state to the HTML attributes, so the attributes do actually appear in that browser.
IE 通过 (a) 获取getAttribute
/setAttribute
错误访问属性而不是属性(这就是为什么你永远不应该在 HTML 文档中使用这些方法),以及 (b) 错误地将当前表单状态映射到HTML 属性,因此这些属性确实会出现在该浏览器中。
but I need selected="selected" there
但我需要 selected="selected" 那里
Why? Are you serialising the form to innerHTML
? This won't include form field values (again, except in IE due to the bug), so is not a usable way to store field values.
为什么?您是否将表单序列化为innerHTML
?这将不包括表单字段值(同样,由于错误而在 IE 中除外),因此不是存储字段值的可用方法。
回答by letronje
This works as expected, Check it out : http://jsfiddle.net/MZYN7/1/
这按预期工作,检查一下:http: //jsfiddle.net/MZYN7/1/
回答by Jakub
It should be $('.test').attr('selected', true);?
它应该是 $('.test').attr('selected', true);?
also
还
<option selected>value</option>
<option selected>value</option>
It will not show up as selected="selected"
它不会显示为 selected="selected"