2013-11-10 45 views
3

我使用这个HTML:取出蜱,如果确认提示假

<tr> 
    <td class="smalltext" width="100%"> 
    <label> 
     <input type="checkbox" value="1" name="guarantee" id="guarantee_check_tick" /> 
     &mdash; Tick this checkbox if you are going to give 100% satisfaction guarantee. 
    </label> 
    </td> 
</tr> 

和jQuery:

jQuery(document).ready(function($) 
{ 
    $("#guarantee_check_tick").click(function() 
    { 
     var $this = $(this); 
     if ($this.is(":checked")) 
     { 
      confirm('Are you sure you are going to provide 100% satisfaction guarantee?'); 
     } 
    }); 
}); 

现在它做什么,当复选框用户点击,一个确认显示(这很好),当用户点击“确定”按钮时勾选复选框,如果用户点击“取消”按钮(在确认提示上),则该复选框应该保持不勾选。我怎样才能做到这一点?

请帮忙!

回答

4

confirm方法返回布尔值。检查它是否为真并执行。

试试这个:

$(document).ready(function() 
{ 
    $("#guarantee_check_tick").change(function() // changed from click to change 
    { 
     var $this = $(this); 
     if ($this.is(":checked")) 
     { 
      if(confirm('Are you sure you are going to provide 100% satisfaction guarantee?') == true){ 
       //your code 
      } 
      else{ 
       $this.removeAttr("checked"); 
      } 
     } 
    }); 
}); 

Fiddle here.

+0

感谢Hiral,这是完美的!我在5分钟内接受你的回答,因为这里的系统不允许我接受atm。 非常感谢你的时间。 – user2854563

+0

@ user2854563欢迎您:) – Hiral

2

工作演示http://jsfiddle.net/88LHL/

文件:https://developer.mozilla.org/en-US/docs/Web/API/Window.confirm

这将满足您的需要:)

代码

jQuery(document).ready(function ($) { 
    $("#guarantee_check_tick").click(function() { 
     var success = false; 
     var $this = $(this); 
     if ($this.is(":checked")) { 
      success = confirm('Are you sure you are going to provide 100% satisfaction guarantee?'); 
     } 


     if (success == true) { 
      alert('Changed'); 

      // do something     
     } else { 

      alert('Not changed'); 
      $this.prop('checked', !$this.prop('checked')); 
      // Cancel the change event and keep the selected element 
     } 

    }); 
}); 
+0

谢谢tats_innit – user2854563

+0

@ user2854563不用担心':''很高兴帮助你! –

2

你可以试试这个,

jQuery(document).ready(function($) 
    { 
     $("#guarantee_check_tick").click(function() 
     { 
      var $this = $(this); 
      if ($this.is(":checked")) 
      { 
       var r = confirm('Are you sure you are going to provide 100% satisfaction guarantee?'); 
        // r value is either true or false 

       if(!r){ 
        $this.attr('checked', false); 
       } 
      } 
     }); 
    }); 
+0

谢谢你,Chinnu R,你也是。 – user2854563