jQuery 无法设置“selected”=“selected”通过

发布于 2024-09-19 23:46:17 字数 480 浏览 4 评论 0原文

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事与诗 2024-09-26 23:46:18

它应该是 $('.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"

迷途知返 2024-09-26 23:46:17

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 属性,因此这些属性确实出现在该浏览器中。

但我需要 selected="selected" 那里

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

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.

The same goes for value vs defaultValue on <input type="text">/<textarea>, and checked/defaultChecked on 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, 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 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.

but I need selected="selected" there

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.

似梦非梦 2024-09-26 23:46:17

这按预期工作,请查看:http://jsfiddle.net/MZYN7/1/

This works as expected, Check it out : http://jsfiddle.net/MZYN7/1/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文