如何过滤掉空值和没有值属性的输入元素?

时间:2014-09-29 10:21:06

标签: jquery

我有这个jQuery函数提交搜索表单并禁用空字段,因此它们不会随表单一起提交,因此不会被添加到URL中的查询字符串中:

$('form#search a#submit').on('click', function(event) {
    $('input').filter(function() { return $(this).val() == ""; }).prop('disabled', true);
    $('form#search').submit();
    event.preventDefault();
});

但问题是,我的select框的默认选项根本没有value属性,因此每次都会使用表单提交,即使它们是空的

如何使用上述函数处理没有value属性的输入元素?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您没有在选择器中选择选择框,请在第2行

上执行此操作
 $('input,select').filter(function() { return $(this).val() == ""; }).prop('disabled', true);