简体   繁体   English

如何使用 JQuery/Javascript 检查选择框是否为空

[英]How to check whether a select box is empty using JQuery/Javascript

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 .我添加了由一个按钮组成的功能,单击该按钮会调用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.如果没有,那么它将什么都不做。

My problem is 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.例如使用document.getElementById(...)...不一定会返回 false,这取决于您如何使用它。 Also using $("#someID")... in jQuery may or may not produce the desired results.同样在 jQuery 中使用$("#someID")...可能会也可能不会产生所需的结果。 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.正如您在注释行中看到的那样,我已经尝试了很多不同的时间,所有这些都可以在if(...)语句中进行评估。

How can this be done?如何才能做到这一点?

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() ) { 

One correct way to get selected value would be获得选定值的一种正确方法是

var selected_value = $('#fruit_name').val()

And then you should do然后你应该做

if(selected_value) { ... }

Another correct way to get selected value would be using this selector:获取选定值的另一种正确方法是使用此选择器:

$("option[value="0"]:selected")

Best for you!最适合你!

Check this answer its tested and working well检查此答案是否经过测试且运行良好

if( !$('#id').val() ) {
   // stuff here
}

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

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