如何使用JQuery / Javascript来检查选择框是否为空 [英] How to check whether a select box is empty using JQuery/Javascript
问题描述
使用JQuery Mobile我有一个从数据库动态填充的选择框。截至目前,这个复选框的人口完美无缺。我添加了一个功能,它包含一个通过点击事件调用功能的按钮。这个函数的作用是知道这个特定的复选框是否已经被填充;如果没有,那么它就不会做任何事情,但否则它会顺利运行。我的问题是确定这个选择框是否为空。
这是我正在处理的一个非常简单的例子:
< li>
< label for =fruit_name>水果< / label>
< / select>
< / li>
从一个单独的按钮调用我的函数,如下所示:
函数isSelextBoxEmpty(selectBoxId){
var selected_value = $('#fruit_name');
/ *更多选项...仍在测试正确的方式:
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);
//做某事
}
其他{
alert(null,value:+ selected_value);
//做某事
}
}
不要担心这是什么以及它是如何做到的。现在对我来说重要的是我不能检查复选框是否为空,我只是不知道该怎么去做。我通过论坛和文档阅读了很多内容,但这样做有很多含义,因为它取决于实施本身。
例如,使用 document.getElementById(...)...
不一定会返回false,它取决于关于你如何使用它。在jQuery中使用 $(#someID)...
可能会或可能不会产生所需的结果。我已经尝试了许多不同的时间,正如你可以在注释行中看到的那样,所有这些都可以在 if(...)
声明。
您对如何实现这一目标有任何建议或想法?提前感谢!
要检查选择框是否有任何值:
if($('#fruit_name')。has('option')。length> 0){
检查所选值是否为空:
if(!$ ('#fruit_name')。val()){
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!
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() ) {
这篇关于如何使用JQuery / Javascript来检查选择框是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!