如何使用 JQuery/Javascript 检查选择框是否为空 [英] How to check whether a select box is empty using JQuery/Javascript

查看:25
本文介绍了如何使用 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.

我添加了由一个按钮组成的功能,该按钮在点击时调用 isSelextBoxEmpty.这个函数的工作是知道这个特定的复选框是否已经被填充;如果没有,那么它什么都不做.

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.

我的问题是确定这个选择框是否为空.

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.

例如使用 document.getElementById(...)... 不一定会返回 false,这取决于您如何使用它.同样在 jQuery 中使用 $("#someID")... 可能会也可能不会产生所需的结果.正如您在注释行中看到的那样,我已经尝试了很多次,所有这些都可以在 if(...) 语句中计算.

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.

您对如何实现这一目标有任何建议或想法吗?提前致谢!

Do you have any suggestions or ideas on how to go about achieving this? Thanks in advance!

推荐答案

检查选择框是否有值:

if( $('#fruit_name').has('option').length > 0 ) {

检查选中的值是否为空:

To check whether selected value is empty:

if( !$('#fruit_name').val() ) { 

这篇关于如何使用 JQuery/Javascript 检查选择框是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆