简体   繁体   English

为什么javascript在我的选择选项上看不到“ selected”属性?

[英]Why does javascript not see the “selected” attribute on my select option?

Short version: 简洁版本:

After inserting a select element with options into the DOM with an AJAX call the "selected" attribute on my option isn't recognized by the JavaScript. 在使用AJAX调用将带有选项的select元素插入DOM后,JavaScript无法识别我选项的“ selected”属性。 It's as if it's not there even though looking at it in Firebug I can see that it is. 即使在Firebug中查看它,好像它也不存在,我可以看到它确实存在。

Long version: 长版:

I'm using a jQuery plugin that generates a "multi select" (select imitation with checkboxes pretty much) based on an actual html select with options that can have a "selected" attribute. 我正在使用一个jQuery插件,该插件基于实际的html select生成可以具有“ selected”属性的选项,从而生成“ multi select”(非常类似于带复选框的select模仿)。 The plugin checks if the selected attribute exists by doing 插件通过执行以下操作检查所选属性是否存在

$(this).attr('selected') == true

and if it exists it sets the corresponding checkbox to checked. 如果存在,则将相应的复选框设置为选中。 This all works when I load the page normally. 当我正常加载页面时,这一切正常。 I also need to reload the data if the user chooses to filter it and here is where the problem is. 如果用户选择过滤数据,我也需要重新加载数据,这就是问题所在。 The new data is fetched via AJAX and the plugin is reapplied to new markup but the "selected" attribute isn't considered present even though it's there just like before . 新数据通过AJAX提取,并且插件重新应用于新标记,但是“ selected”属性不被视为存在, 即使它像以前一样也存在 Other attributes work fine but "selected" bizarrely enough doesn't. 其他属性工作正常,但是“选择”足够奇怪。 This is the markup both when it works and when it doesn't. 无论何时有效,这都是标记。

<option value="1" selected="selected" warning="true"> text</option>

I'm really scratching my head over this. 我真的为此挠头。 Anyone got a suggestion? 有人有建议吗?

Forget jQuery's confusingly named attr() method and attributes in general, and instead just use the selected property of the option, which is a Boolean and works in all major browsers released since 1996: 一般情况下,不要使用jQuery令人困惑的attr()方法和属性,而只需使用该选项的selected属性即可,该属性是一个布尔值,可在1996年以来发布的所有主要浏览器中使用:

// Assuming the select list is stored in a variable called select
alert( select.options[0].selected );

If you must use jQuery to get the option, here's the equivalent: 如果必须使用jQuery来获取选项,则等效项如下:

alert( $(this)[0].selected );

Attributes are almost never what you want, since they are incorrectly implemented in IE and only ever represent the initial value of a some aspect of the element, not the current value. 属性几乎从来都不是您想要的,因为它们在IE中错误地实现,并且仅表示元素某些方面的初始值,而不是当前值。 jQuery's attr() method generally uses properties internally. jQuery的attr()方法通常在内部使用属性。

why are you comparing to true ? 你为什么要与true比较? the "selected" value is "selected" , not true . "selected"值是"selected" ,不是true

您获得的元素在其中Selected == True但已选定的地方实际上是“已选定”。

A better way to check if an option is selected, type the following: 检查是否选择了一个更好的方法,键入以下内容:

if ($(this).is(":selected")) {
 ...
}

So, if you want to detect all selected options you could use: 因此,如果要检测所有选定的选项,可以使用:

$("select option:selected").each(function () { ... }); $(“ select option:selected”)。each(function(){...});

Perhaps, this fixes the problem. 也许,这可以解决问题。

I figured out what the problem was. 我发现了问题所在。 We use a js combiner and apparently moving the include call to a different file fixed the problem. 我们使用了一个js组合器,并且显然将include调用移到了另一个文件,从而解决了该问题。

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

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