如何防止表单提交 JQuery
How to prevent form submit JQuery
我想检查是否所有复选框都没有选中,这将阻止提交表单。例如,我在表单中有 7 个复选框(天),如果用户单击提交按钮而不选中其中一个复选框,它将显示一条错误消息并阻止提交表单。如果用户选中其中一个复选框,它将运行下面的 if-else,如果一切正常,它将插槽减少 1。我知道怎么做吗?
JQuery 代码
var slot = 7;
$(".day").each(function(){
if($(this).is(":checked")){
var num = $('.day').index(this);
if($("[name='sd_input_dosage_value[]']").eq(num).val() == ""){
alert("The dosage is required and cannot be empty.");
return false;
}
else if(!$.isNumeric($("[name='sd_input_dosage_value[]']").eq(num).val())){
alert("The dosage is not a number.");
return false;
}
else if(parseFloat($("[name='sd_input_dosage_value[]']").eq(num).val()) < 0){
alert("The dosage cannot be a negative value.");
return false;
}
else if($("[name='sd_dosage_select_value[]']").eq(num).val() == ""){
alert("The dosage unit cannot be empty.");
return false;
}
else if($("[name='sd_input_quantity_value[]']").val() == ""){
alert("The quantity is required and cannot be empty.");
return false;
}
else if(!$.isNumeric($("[name='sd_input_quantity_value[]']").val())){
alert("The quantity is not a number.");
return false;
}
else if(parseFloat($("[name='sd_input_quantity_value[]']").val()) < 0){
alert("The quantity cannot be a negative value.");
return false;
}
else{
slot = slot -1;
}
}
});
if(slot == 7){
alert("There must be a least one check box checked.");
}
您可以使用event.preventDefault()
来阻止表单提交;
基于HTML
结构的假设:
// When form is submitted
$('form').on('submit', function(e) {
// Get the number of checked checkboxes
if ($('input[name="days[]"]:checked').length) {
// Your code here
} else {
// Prevent form from submitting
e.preventDefault();
}
});
$("form").submit(function(){
$(":input[type ^= checkbox]").each(function(){
if(!$(this).is(":checked"))
{
alert("Message");
return false;
}
});
})
嗨,
如果您试图阻止提交表单,请尝试这个。
$(function(){
$("#formid").submit(function(event){
var slot = 7;
$(".day").each(function(){
if($(this).is(":checked")){
var num = $('.day').index(this);
if($("[name='sd_input_dosage_value[]']").eq(num).val() == ""){
alert("The dosage is required and cannot be empty.");
return false;
}else if(!$.isNumeric($("[name='sd_input_dosage_value[]']").eq(num).val())){
alert("The dosage is not a number.");
return false;
}else if(parseFloat($("[name='sd_input_dosage_value[]']").eq(num).val()) < 0){
alert("The dosage cannot be a negative value.");
return false;
}else if($("[name='sd_dosage_select_value[]']").eq(num).val() == ""){
alert("The dosage unit cannot be empty.");
return false;
}else if($("[name='sd_input_quantity_value[]']").val() == ""){
alert("The quantity is required and cannot be empty.");
return false;
}else if(!$.isNumeric($("[name='sd_input_quantity_value[]']").val())){
alert("The quantity is not a number.");
return false;
}else if(parseFloat($("[name='sd_input_quantity_value[]']").val()) < 0){
alert("The quantity cannot be a negative value.");
return false;
}else{
slot = slot -1;
}
}
});
if(slot == 7){
alert("There must be a least one check box checked.");
event.preventDefault();
}
});
});
相关文章:
- setTimeOut AFTER jQuery表单提交
- jquery表单提交无法使用多个输入(IE和Firefox)
- Jquery表单提交仍在刷新
- 如何触发我的 jquery 表单提交
- 防止在 Jquery 表单提交时加载页面,但“无”显示按钮
- Jquery表单提交行为不可理解
- 捕获 jQuery 表单提交事件
- 为什么 Jquery 表单提交事件不触发
- jQuery 表单提交事件处理程序不会被调用(chrome 扩展)
- 轨道.jQuery表单提交字段模糊
- JQuery表单提交没有'使用回调函数时无法工作
- Jquery表单提交不起作用
- jQuery表单提交是't提交表格
- JQuery表单提交在IE7&IE8
- jQuery表单提交没有't似乎触发if语句
- jQuery表单提交后的冗余参数
- 当URL中存在查询字符串时,Jquery表单提交无法工作
- Jquery表单提交返回false不起作用
- 在没有php的情况下使用jquery表单提交是否足够安全
- 可以't让jQuery表单提交工作