Jquery 检查表单以查看任何输入是否具有值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10413716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery check form to see if any inputs has value
提问by user1253239
if ($('#requestForm').val == "") {
alert('Please select at least one filter');
//return false;
}
else {
///Run my code
}
I have a form, no inputs are required, but if no inputs are selected I want an alert to tell them to select at least one option.
我有一个表格,不需要输入,但如果没有选择输入,我想要一个警报,告诉他们至少选择一个选项。
采纳答案by Dattatray Walunj
var validate= false;
$('#requestForm input').each(function(){
if($(this).val() != '' || $(this).attr('checked'))
validate = true;
});
if(!validate){
alert('Please select at least one filter');
return false;
}
else {
Run my code
}
回答by gdoron is supporting Monica
var isValid = !!$('form :input').filter(function() {
return this.value;
}).length;?
if (!isValid)
alert('Please select at least one filter');
If the form id is requestForm
use it as the form selector:
如果表单 idrequestForm
用作表单选择器:
$('#requestForm :input').filter(function() {
...
回答by KyleMit
Neither of the current answers benefit from short circuiting as soon the condition is met. This probably only amounts to a marginal hit on performance for a finite number of fields in JavaScript. But also, neither are great at declaratively stating the intention of the loop you're entering.
一旦满足条件,当前的答案都不会从短路中受益。对于 JavaScript 中的有限数量的字段,这可能只是对性能的轻微影响。而且,两者都不擅长声明性地说明您正在进入的循环的意图。
Here's a simple 4 line extension method that takes in any filter criteria and returns true as soon as something matches that criteria.
这是一个简单的 4 行扩展方法,它接受任何过滤条件并在符合该条件时立即返回 true。
jQuery.fn.any = function(filter){
for (i=0 ; i<this.length ; i++) {
if (filter.call(this[i])) return true;
}
return false;
};
Then use it like this
然后像这样使用它
var gotMatch = $(":input").any(function() {
// you can put any condition you want in here
return this.value == 'hi';
});
jQuery.fn.any = function(filter){
for (i=0 ; i<this.length ; i++) {
if (filter.call(this[i])) return true;
}
return false;
};
$(function() {
var gotMatch = $(":input").any(function() {
return this.value == 'hi';
});
if (gotMatch) {
console.log("Hello to you too!");
} else {
console.log("Why don't you say Hi!");
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="">
<input type="text" value="">
<input type="text" value="">
Demo in jsFiddle
jsFiddle 中的演示
Further Reading:
进一步阅读:
- jQuery -
.has()
- jQuery -
.filter()
- SO - "Any" boolean function in jquery
- SO - Is there an "exists" function for jQuery?
- SO - jQuery check if any text input has value
- jQuery -
.has()
- jQuery -
.filter()
- SO - jquery 中的“任何”布尔函数
- SO - jQuery 有“存在”函数吗?
- SO - jQuery 检查是否有任何文本输入具有值
回答by abx78
It's not so clear to me what you're trying to achieve but maybe you can find useful this
我不太清楚您要实现的目标,但也许您可以找到有用的
https://github.com/maxatwork/form2js
https://github.com/maxatwork/form2js
It's a library that converts structured form data into Javascript objects. You only have to do this
它是一个将结构化表单数据转换为 Javascript 对象的库。你只需要这样做
var obj = form2js("requestForm");
and then parse the result and warn the user if nothing has been selected.
然后解析结果并警告用户如果没有选择任何内容。
回答by Kareem
check = function()
{
var empty = $("#requestForm :input").filter(function() {
return this.value == ""; });
if(empty.length) {
alert("Please select at least one filter");
};}
This will alert the message ONCE if at least one input is empty. then simply add the function onclick to your submit input or any button you want to validate the form with.
如果至少有一个输入为空,这将提醒消息 ONCE。然后只需将 onclick 函数添加到您的提交输入或您想要验证表单的任何按钮。
<input type="submit" onclick="check();">