使用 jQuery 检查选定的下拉值是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23218929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if selected dropdown value is empty using jQuery
提问by Domas
Here is the dropdown in question:
这是有问题的下拉列表:
<select name="data" class="autotime" id="EventStartTimeMin">
<option value=""></option>
<option value="00">00</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
<option value="50">50</option>
</select>
What I want to do is check if the current value is empty:
我想要做的是检查当前值是否为空:
if ($("EventStartTimeMin").val() === "") {
// ...
}
But it does not work, even though the value is empty. Any help is much appreciated.
但它不起作用,即使该值为空。任何帮助深表感谢。
回答by Rory McCrossan
You forgot the #
on the id selector:
你忘记了#
id 选择器:
if ($("#EventStartTimeMin").val() === "") {
// ...
}
回答by Rajat_Kumar_India
You can try this also-
你也可以试试这个——
if( !$('#EventStartTimeMin').val() ) {
// do something
}
回答by Felix
You need to use .change()
event as well as using #
to target element by id
:
您需要使用.change()
event 以及使用以下#
方式定位元素id
:
$('#EventStartTimeMin').change(function() {
if($(this).val()===""){
console.log('empty');
}
});
回答by Satish Dhoni
Try this it will work --
试试这个它会起作用——
if($('#EventStartTimeMin').val() === " ") {
alert("Please enter start time!");
}