2012-03-06 71 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> 

我希望它能帮助!