2013-11-10 43 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