如何防止表单提交JQuery

时间:2015-06-19 07:46:25

标签: javascript jquery forms

我希望检查是否未选中所有复选框,这将阻止表单被提交。例如,我在表单中有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.");
}

3 个答案:

答案 0 :(得分:5)

您可以使用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();
    }
});

答案 1 :(得分:0)

$("form").submit(function(){
   $(":input[type ^= checkbox]").each(function(){
      if(!$(this).is(":checked"))
      {
         alert("Message");
         return false;   
      }  
   });
})

答案 2 :(得分:0)

如果您试图阻止提交表单,请尝试使用此表单。

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