2015-06-19 250 views
-2

我想检查是否所有的复选框没有被选中,它会阻止表单被提交。例如,我在表单中有7个复选框(天),并且如果用户单击提交按钮而没有选中其中一个复选框,它将显示错误消息并阻止表单被引用。如果用户选中了其中一​​个复选框,它将运行if-else below,如果一切正常,它会将插槽减少1.我的想法是怎么做的?如何防止表单提交JQuery

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

http://stackoverflow.com/questions/6462143/prevent-default-on-form-submit-jquery的可能重复 –

回答

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(); 
    } 
}); 
0
$("form").submit(function(){ 
    $(":input[type ^= checkbox]").each(function(){ 
     if(!$(this).is(":checked")) 
     { 
     alert("Message"); 
     return false; 
     } 
    }); 
}) 
0

嗨,如果你试图阻止表单被提交你试试这个。

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