![](/img/trans.png)
[英]Cannot get selectedIndex from select element positioned in cell in hidden row
[英]Return SelectedIndex From Select Element
如果未选择任何内容,而不是位置0的元素文本,是否有可能让selectedIndex
返回-1。
另外, selectedIndex
返回0,即使没有被选中。
<select id="qwe" name="qwe">
<option>11</option>
<option>22</option>
</select>
document.someForm.qwe.value
//returns 11
$('#qwe option:selected').val()
//returns 11
<select id="qwe" name="qwe">
<option selected="false">11</option>
<option>22</option>
</select>
$('#qwe option:selected').val()
//returns 11
<select id="qwe" name="qwe">
<option selected="false">11</option>
<option selected="false">22</option>
</select>
$('#qwe option:selected').val()
//returns 22
无需添加默认的第一个选项...只需使用.prop()
并更改selectedIndex
:
$('#qwe').prop('selectedIndex', -1);
演示: http : //jsfiddle.net/dirtyd77/j76yH/2/
如果您有任何疑问,请告诉我!
选择框必须始终选择一个值(默认情况下,第一个选项)。 如果您想要这种行为,只需在列表顶部添加一个空选项,并添加所需的任何值(例如-1)。
这个怎么样?
<select id="demo">
<option value="-1">---</option>
<option value="1">1</option>
<option value="2">1</option>
</select>
正如hexblot所说,每个选择框都有一个选定值。 总是..;)
尝试使用以下语句:
if ($('#qwe option[selected="selected"]').val() === undefined) return -1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.