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

3 回答 3

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");
            }
        }
    });
});

在这里拉小提琴。

于 2013-11-10T08:18:01.500 回答
3

工作演示 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
        }

    });
});
于 2013-11-10T08:20:18.717 回答
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);
                }
            }
        });
    });
于 2013-11-10T08:22:50.147 回答