2015-06-19 302 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(); 
 
    } 
 
    }); 
 
});