2012-03-06 78 views
3
<select name="status" id='status'> 
    <option value="Submitted">Submitted</option> 
    <option value="Canceled">Canceled</option> 

</select> 
<input type="checkbox" id="app_box" name="application_complete" value="checked"> App Complete 

<script type="text/javascript"> 
$(function() { 

    $('form.editable').submit(function(){ 
     if ($('#status').val()=='Canceled') { 
      if (!confirm('This information will be discarded!)) { 
       return false; 
      } 
     } 
    }); 

}); 
</script> 

之前在jQuery的檢查所以,我有正常工作上面的腳本。我必須再添加一個確認。當代理點擊提交按鈕時,我想檢查應用程序複選框是否被選中。如果未檢查,則顯示另一個確認框,說明您必須選中該框。這怎麼可以在jQuery中完成。如何驗證是否一個複選框被提交

+0

可能的重複 - http://stackoverflow.com/questions/426258/how-do-i-check-a-checkbox-with-jquery-or-javascript – scibuff 2012-03-06 16:48:52

回答

10

像這樣:

if ($('#app_box').is(':checked')){...} 

或者

if ($('#app_box')[0].checked){...} 

因此,這裏是如何你的代碼笑ULD是:

$('form.editable').submit(function(){ 
    if (! $('#app_box')[0].checked){ 
     alert('Check App Complete first !'); 
     return false; 
    } 

    if ($('#status').val() == 'Canceled') { 
     if (!confirm('This information will be discarded!')) { 
      return false; 
     } 
    } 
}); 

瞭解更多:

+0

好吧我有一個愚蠢的問題,我沒有在文檔中找到。我如何檢查複選框是否未選中? – Micheal 2012-03-06 17:06:07

+0

$(「#app_box」)。!is(':checked')??? – Micheal 2012-03-06 17:09:45

+0

好的,非常感謝 – Micheal 2012-03-06 17:10:09

1

:checked

(文檔是你最好的朋友......)

+0

另外,一個簡單的谷歌搜索作爲'jquery複選框選中'給出了很多結果。第一個是http://stackoverflow.com/questions/901712/check-checkbox-checked-property-using-jquery – DG3 2012-03-06 16:49:49

+0

真的......當嚴格時,這應該是-1這個問題... – Christoph 2012-03-06 16:50:42

0
$("input[name='application_complete']").is(":checked")) 
0

你可以嘗試這樣的事:

<script type="text/javascript"> 
$(function() { 

    $('form.editable').submit(function(){ 
     if ($('#status').val()=='Canceled') { 

      //check if the box is checked! 
      if ($("#app_box").is(':checked')) { 
       alert("You have to check the box 'App Complete'"); 
       return false; 
      } 

      if (!confirm('This information will be discarded!)) { 
       return false; 
      } 
     } 
    }); 

}); 
</script> 

我希望它能幫助!