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

*爱你&永不变心* 提交于 2019-11-30 11:21:13

问题


Using JQuery Mobile 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 calls a function via an 'on click' event. The job of this function is to know whether this particular check box has been populated or not; if it hasn't then it will simply not do anything, but otherwise it will run smoothly. My problem is determining whether or not 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
    }
    else {
        alert("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.

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


回答1:


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



回答2:


One correct way to get selected value would be

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

And then you should do

if(selected_value) { ... }



回答3:


Another correct way to get selected value would be using this selector:

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

Best for you!



来源:https://stackoverflow.com/questions/11039658/how-to-check-whether-a-select-box-is-empty-using-jquery-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!