我认为我的选择/选项标签有问题...我无法访问选定的选项。选择选项 - 无法获取选项
这里的HTML部分:
<select id="filter">
<option value="value1" selected="selected" data-path="property1">option1</option>
<option value="value2" data-path="property2">option2</option>
</select>
我试着这样说:
var property_filter = document.getElementById('filter');
而且在调试时,在控制台上,这是我的尝试:
input: property_filter.options
output: [<option value="value1" selected="selected" data-path="property1">option1</option>,<option value="value2" data-path="property2">option2</option>]
所以它抓住了正确的选择标签...但仍有一些错误:
input: property_filter.selectedIndex
output: -1
同样,当我使用JQuery选择器来找到这个,我没有得到我想要的。
input: $('#filter').find('option')
output: [<option value="value1" selected="selected" data-path="property1">option1</option>, <option value="value2" data-path="property2">option2</option>]
input: $('#filter').find('option:selected')
output: []
我一定在做错事情......这也是一件很简单的事情。我也尝试使用'选择',而不是'选择='选择'',但我得到了同样的结果。
我在OS 10.6.8上顺便使用了Chrome,并且在Backbone View & QUnit测试的环境下(但它没关系,对不对?)。此外,此代码在QUnit之外工作。
在JavaScript中,其'selectedIndex',在jQuery中它是':selected'。 – j08691
尝试.hasAttribute(“selected”);检查它是否被设置,也许你正在修改它 – RyanS
http://jsfiddle.net/mqefD/1/看起来不错。 – jAndy