如何使用 JQuery/Javascript 检查选择框是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11039658/
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
How to check whether a select box is empty using JQuery/Javascript
提问by AGE
I have a select box that is being populated dynamically from a database. As of right now the population of this check box is working flawlessly.
我有一个从数据库动态填充的选择框。截至目前,此复选框的人口工作完美无缺。
I have added functionality that consists of a button which on click calls isSelextBoxEmpty
. The job of this function is to know whether this particular check box has been populated or not; if it has not then it will simply do nothing.
我添加了由一个按钮组成的功能,该按钮在点击时调用isSelextBoxEmpty
。这个函数的工作是知道这个特定的复选框是否已经被填充;如果没有,那么它什么都不做。
My problemis in determining whether this select box is empty or not.
我的问题是确定这个选择框是否为空。
Here is a very simplified example of what I am dealing with:
这是我正在处理的一个非常简单的例子:
<li>
<label for="fruit_name">Fruit</label>
<select name="some_fruit" id="fruit_name" onclick="populate_box('fruit', this);">
</select>
</li>
My function, which is called from a separate button, looks like this:
我的函数是从一个单独的按钮调用的,如下所示:
function isSelextBoxEmpty(selectBoxId) {
var selected_value = $('#fruit_name');
/* More options... still testing the proper way:
var selected_value = $('#fruit_name').text;
var selected_value = $('#fruit_name').value;
var selected_value = $('#fruit_name').length;
var selected_value = $('#fruit_name option:selected', this);
var selected_value = document.getElementById('fruit_name');
var selected_value = document.getElementById('fruit_name').length;
var selected_value = document.getElementById('fruit_name').value;
var selected_value = document.getElementById('fruit_name').innerHTML;
*/
if (selected_value) {
alert("NOT null, value: " + selected_value);
// do something
}
}
Don't worry about what this does and how it does it. Right now what matters to me is that I can't check whether or not the checkbox is empty, I am just not sure how to go about it. I have read a lot through forums and documentation but there are many implications in doing this since it depends on the implementation itself.
不要担心这是做什么以及它是如何做的。现在对我来说重要的是我无法检查复选框是否为空,我只是不知道如何去做。我通过论坛和文档阅读了很多,但这样做有很多含义,因为它取决于实现本身。
For instance using document.getElementById(...)...
will not necessarily return false and it depends on how you use it. Also using $("#someID")...
in jQuery may or may not produce the desired results. I have already tried many different times as you can see in the commented lines, all of which can be evaluated in the if(...)
statement.
例如 usingdocument.getElementById(...)...
不一定会返回 false,这取决于您如何使用它。同样$("#someID")...
在 jQuery 中使用可能会也可能不会产生所需的结果。正如您在注释行中看到的那样,我已经尝试了许多不同的时间,所有这些都可以在if(...)
语句中进行评估。
Do you have any suggestions or ideas on how to go about achieving this? Thanks in advance!
您对如何实现这一目标有任何建议或想法吗?提前致谢!
回答by Engineer
To check whether select box has any values:
要检查选择框是否有任何值:
if( $('#fruit_name').has('option').length > 0 ) {
To check whether selected value is empty:
检查所选值是否为空:
if( !$('#fruit_name').val() ) {
回答by Claudio Redi
One correct way to get selected value would be
获得选定值的一种正确方法是
var selected_value = $('#fruit_name').val()
And then you should do
然后你应该做
if(selected_value) { ... }
回答by gmpacheco
Another correct way to get selected value would be using this selector:
获取选定值的另一种正确方法是使用此选择器:
$("option[value="0"]:selected")
Best for you!
最适合你!