jQuery不能设置"selected" ="selected".通过< option>上的attr()元素? [英] jQuery Cannot set "selected"="selected" via attr() on <option> elements?

查看:139
本文介绍了jQuery不能设置"selected" ="selected".通过< option>上的attr()元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<select>
    <option>1</option>
    <option>2</option>
    <option class="test">3</option>
</select>

$('.test').attr('selected', 'selected');​

上面的选择框将默认选择第三个选项,而不会出现任何问题. 但是,如果您使用Firebug检查元素,则所选的<option>中将不存在任何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>.

我知道这不是一个大问题,因为它仍然可以正常运行,但是我需要在其中运行selected="selected",以便其他脚本可以捕获并执行进一步的处理.有解决方法吗?

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?

谢谢.

推荐答案

selected属性对应于该选项的当前选择状态. selected属性对应于 default 所选内容,如果在表单上调用.reset()(或单击type="reset"按钮),则该属性将恢复.可以使用DOM属性selected访问当前选择状态,而通过属性反映的默认选择状态可以在DOM属性defaultSelected下访问.

The selected attribute does not correspond to the current selectedness state of the option. The selected attribute corresponds to the default selectedness, 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.

<input type="text">/<textarea>上的valuedefaultValue以及在type="checkbox"/"radio"上的checked/defaultChecked上也是如此.

The same goes for value vs defaultValue on <input type="text">/<textarea>, and checked/defaultChecked on type="checkbox"/"radio".

jQuery的attr()的名称具有误导性,它试图假装属性和属性是同一件事是有缺陷的.使用attr()时,通常会访问属性,而不是属性.因此,$(el).attr('selected', 'selected')实际上正在执行el.selected= 'selected'.之所以起作用,是因为'selected'与任何非空字符串一样都是'truthy':它被转换为el.selected= true.但这一点都没有触及selected="selected"属性.

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, not the 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.

IE通过使(c)错误的getAttribute/setAttribute错误,从而访问属性而不是属性(这就是为什么您永远不要在HTML文档中使用这些方法的原因),进一步使这种本已令人困惑的情况变得更加复杂. b)错误地将当前表单状态映射到HTML属性,因此这些属性实际上确实出现在该浏览器中.

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.

但是我需要在这里选择"selected"(选择)

but I need selected="selected" there

为什么?您要将表格序列化为innerHTML吗?该字段将不包括表单字段值(同样,由于该错误,IE中除外),因此这不是存储字段值的可用方法.

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.

这篇关于jQuery不能设置"selected" ="selected".通过&lt; option&gt;上的attr()元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆