jQuery 无法设置“selected”=“selected”通过
<select>
<option>1</option>
<option>2</option>
<option class="test">3</option>
</select>
$('.test').attr('selected', 'selected');
上面的选择框将默认选择第三个选项,没有任何问题。 但是,如果您使用 Firebug 检查元素,则所选 中不存在任何
selected="selected"
属性。
我知道这不是一个大问题,因为它仍然可以工作,但我需要 selected="selected"
,以便我的其他脚本可以捕获它并执行进一步的处理。有没有解决方法?
谢谢。
<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>
.
I know this isn't a big issue as it still works but I need selected="selected"
there so my other scripts could catch it and perform further processing. Are there any workarounds out there?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它应该是
$('.test').attr('selected', true);
也
它不会显示为
selected="selected"
It should be
$('.test').attr('selected', true);
also
<option selected>value</option>
It will not show up as
selected="selected"
selected
属性不对应于选项的当前选中状态。selected
属性对应于默认选择状态,如果调用.reset()
(或者type="单击表单上的“重置”
按钮)。当前的选定状态可以使用 DOM 属性selected
访问,而属性反映的默认选定状态则在 DOM 属性defaultSelected
下访问。/
jQuery 的
attr()
的命名具有误导性,并且它试图假装属性和特性是同一事物的尝试是有缺陷的。当您使用attr()
时,您通常访问的是属性,而不是属性。因此,$(el).attr('selected', 'selected')实际上是在执行el.selected='selected'
。这是有效的,因为'selected'
与任何非空字符串一样,是“truthy”:它会转换为el.selected= true
。但这在任何时候都不会触及selected="selected"
属性。IE 使这个已经令人困惑的情况进一步复杂化,因为 (a) getAttribute/setAttribute 错误,因此它访问的是属性而不是属性(这就是为什么你永远不应该在HTML 文档),以及 (b) 错误地将当前表单状态映射到 HTML 属性,因此这些属性确实出现在该浏览器中。
为什么?您是否将表单序列化为
innerHTML
?这不会包括表单字段值(同样,由于错误而在 IE 中除外),因此不是存储字段值的可用方法。The
selected
attribute does not correspond to the current selectedness state of the option. Theselected
attribute corresponds to the default selectedness, which will be restored if.reset()
is called (or atype="reset"
button is clicked) on the form. The current selectedness state can be accessed using the DOM propertyselected
, whereas the default-selectedness, as reflected by the attribute, is accessed under the DOM propertydefaultSelected
.The same goes for
value
vsdefaultValue
on<input type="text">
/<textarea>
, andchecked
/defaultChecked
ontype="checkbox"
/"radio"
.jQuery's
attr()
is misleadingly named, and its attempts to pretend attributes and properties are the same thing is flawed. When you useattr()
, you are usually accessing the property, not the attribute. Consequently,$(el).attr('selected', 'selected')
is actually doingel.selected= 'selected'
. This works because'selected'
, as with any non-empty string, is ‘truthy’: it gets converted toel.selected= true
. But this does not at any point touch theselected="selected"
attribute.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.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.这按预期工作,请查看:http://jsfiddle.net/MZYN7/1/
This works as expected, Check it out : http://jsfiddle.net/MZYN7/1/