在JQuery中检查列表框是否为空的安全方法是什么?备选问题,如何检查是否没有主动选择项目?
我试过了 :
if(... .val() != "null")
Run Code Online (Sandbox Code Playgroud)
也
if(... .val() != null)
Run Code Online (Sandbox Code Playgroud)
因为当列表为空或没有选择任何内容时我在调试时发现了null值val(),但似乎没有准确工作.
要检查<option>元素中是否有<select>元素:
if ($('#my-select').children().length == 0) {
alert('OH NO, there aren\'t any options to select!');
}
Run Code Online (Sandbox Code Playgroud)
检查是否已选择<option>内部<select>(仅当所选<option>元素具有value属性时才有效)
if (typeof($('select').children(':checked').val()) == 'undefined') {
alert('No Option with a Value Selected');
}
Run Code Online (Sandbox Code Playgroud)
这是一个演示:http://jsfiddle.net/sJdBJ/
一些文件:
typeof():https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof.children():http://api.jquery.com/children.length:http://api.jquery.com/length| 归档时间: |
|
| 查看次数: |
4763 次 |
| 最近记录: |