简体   繁体   English

如何防止表单提交JQuery

[英]How to prevent form submit JQuery

I wish to check if all the checkbox is not checked, it will prevent the form being submited.我想检查是否所有复选框都没有选中,这将阻止提交表单。 For example, I have 7 checkboxs(days) in the form and if user click submit button without check one of the checkboxs, it will display an error message and prevent the form being submited.例如,我在表单中有 7 个复选框(天),如果用户单击提交按钮而不选中其中一个复选框,它将显示错误消息并阻止提交表单。 If the user check the one of the check box, it will run the if-else below and if all is ok it will reduce the slot by 1. Any I idea how to do it?如果用户选中复选框之一,它将运行下面的 if-else,如果一切正常,它会将插槽减少 1。我知道该怎么做吗?

JQuery code查询代码

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.");
}

You can use event.preventDefault() to prevent form from submitting;您可以使用event.preventDefault()来防止表单提交;

Based on assumptions of your HTML structure:基于对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;   
      }  
   });
})

Hi if you are trying to prevent the form from being submitted you try this one.嗨,如果您试图阻止提交表单,请尝试这个。

 $(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(); } }); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM