Display alert if checkbox isn't checked on button click [closed]

荒凉一梦 提交于 2019-12-13 09:45:24

问题


How I can issue a pop up alert, such that when a user clicks a button, and a checkbox isn't checked, an alert will appear using jQuery?

The checkbox is:

<input type="checkbox" id="confirm" class="confirm">

The button is:

<form action="resetprocess.php" method="POST">
  <button type="submit" id="reset" class="btn btn-danger">Reset <i class="icon-repeat"></i></button> 
</form>

回答1:


You need to capture the click event of your button and then check whether the checkbox is clicked or not. If not, you can send the alert and return false (be sure to do this otherwise the form will be submitted anyway).

$('#reset').click(function () {
    if (!$('#confirm').is(':checked')) {
        alert('not checked');
        return false;
    }
});

jsFiddle example




回答2:


$("#reset").on('click',function(){
  if(!$("#confirm").is(':checked')){
   // alert("");
  }
});



回答3:


$("#reset").click(function(){
    if ($("#checkbox:checked").length == 0)
        alert("Checkbox not checked!");
});


来源:https://stackoverflow.com/questions/16046953/display-alert-if-checkbox-isnt-checked-on-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!