[英]Check if form has at least one filled / checked / selected element
I would like to check if at least one of form's items has been filled / checked / selected. 我想检查是否已填写/检查/选择了表单的至少一项。 Form uses input fields, checkboxes and radio buttons. 表单使用输入字段,复选框和单选按钮。 At least one of the items should be selected / filled / checked for the form to proceed. 至少应选择/填写/检查其中一项,以进行表格处理。
My solution is the following: 我的解决方案如下:
$('#subform').submit(function(e){
els = $('.filter_ext[type="text"], .filter_ext[type="radio"]:checked, .filter_ext[type="checkbox"], select.filter_ext');
empty_count = 0;
els.each(function(){
th = $(this);
cur_val = th.val();
if(cur_val=='' || !th.checked) {
th.prop('disabled', true); // omit empty items from url get variables (nicer url)
empty_count ++;
}
});
if(empty_count == els.length) { // if all are empty stop form submit
e.preventDefault();
} // else proceed with submitting form
});
Is there a more elegant way to solve this? 有没有更优雅的方式来解决这个问题?
EDIT Thanks to Louys Patrice Bessette 's answer, I simplified with the following code (adding the disablement to his solution): 编辑感谢Louys Patrice Bessette的回答,我简化了以下代码(将禁用功能添加到他的解决方案中):
var serialized_orig = $('#subform').serialize();
$('#subform').submit(function(e){
th = $(this);
serialized = th.serialize();
$.each(serialized.split('&'),function(i,v){
item = v.split('=');
item_name = item[0];
item_value = item[1];
if(item_value == '') {
$('.filter_ext[name="'+item_name+'"]').prop('disabled', true);
}
});
if(serialized == serialized_orig) {
e.preventDefault();
}
});
You could use Array.some()
for detecting that at least one of the elements in the collection contains the condition. 您可以使用Array.some()
来检测集合中的至少一个元素包含条件。 I wrote a vanilla JS example for you - feel free to evaluate it: https://codepen.io/zvona/pen/ywoJxg 我为您编写了一个原始JS示例-请随时对其进行评估: https : //codepen.io/zvona/pen/ywoJxg
It'd look something like this in jQuery (not tested): 在jQuery中看起来像这样(未经测试):
const isValid = $('.formElem').some(($elem) => $elem.val().length || $elem.attr('checked'));
Usually, form validation will have a close look at all required fields to be filled... 通常,表单验证会仔细检查所有需要填写的字段 ...
It is based on an AND logic... 它基于AND逻辑...
Now, your question is something quite unusual: 现在,您的问题很不寻常:
if at least one of form's items has been filled / checked / selected. 如果已填写/检查/选择了表单的至少一项。
That is an OR logic! 那是OR逻辑!
So I came up with a funny idea... 所以我想出了一个有趣的主意...
What if on submit
, you compare the serialised form data with an "original unfilled" one? 如果在submit
,您将序列化表格数据与“原始未填写”数据进行比较?
Just make sure to have a name
attribute on each elements... 只要确保每个元素上都有一个name
属性...
// Original unfilled form data var initialFormValues = $("#subform").serialize(); $("#subform").on("submit",function(e){ if( $(this).serialize() == initialFormValues ){ // Is form data changed? e.preventDefault(); console.log("Not submitted"); }else{ e.preventDefault(); // Just for that snippet! remove that for real. console.log("Submitted"); } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="subform"> <input type="text" name = "text"><br> <input type="checkbox" name="checkbox"><br> <input type="radio" name="radio">1<br> <input type="radio" name="radio">2<br> <select name="select"> <option value="" disabled selected>Choose</option> <option value="1">1</option> <option value="2">2</option> </select><br> <br> <input type="submit" value="Submit"> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.