简体   繁体   English

jQuery:单击前确认操作

[英]Jquery: confirm before click action

I have a checkbox in my form. 我的表格中有一个复选框。 I want to ask the user for confirmation before he checks the box. 我想在用户选中该框之前要求用户确认。 However, the "confirm" message automatically comes up AFTER the box gets ticked. 但是,在框被打勾后,“确认”消息会自动出现。 I want to make it so the user can "cancel", and it's as if he never checked the box to begin with. 我要这样做,以便用户可以“取消”,就好像他从来没有选中该框一样。

Here's my code: 这是我的代码:

$( document ).ready(function() {
  $('.checkme').click(function(){
    text_area_id = "#" + $(this).val();
    if ($(text_area_id).attr('hidden')) {
      $(text_area_id).attr('hidden', false);
    } else{
      if ($(text_area_id).val() != '') {
        if (confirm("Clear text for \'" + $(this).val().replace(/_/g, " ") + "\'?")) {
          $(text_area_id).attr('hidden', true);
          $(text_area_id).val('');
          $(this).attr('checked', true);
          $('.checkme').attr('checked', true);
        }
      } else {
        $(text_area_id).attr('hidden', true);
        $('.checkme').attr('checked', true);
      }
    }
  });

Note the "confirm" box. 注意“确认”框。 I want that to come up upon clicking the checkmark box -- but BEFORE the box actually gets ticked. 我希望在单击复选标记框时出现这种情况-但在该框实际被打勾之前。

So I'm guess I'm looking for a jquery function like 所以我想我正在寻找一个像

.before_click(function(){ ...

instead of 代替

.click(function(){ ...

Or something like that...? 或类似的东西...?

Just set the checked attribute of checkbox based on your confirm boolean value. 只需根据您的confirm boolean值设置复选框的选中属性。 window.confirm returns true or false window.confirm返回truefalse

 $(".checkme").on('click',function(e){ var r = confirm("Are you sure?!"); $(this).attr('checked',r); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" class="checkme"/>Check me 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM