简体   繁体   English

单击取消时如何将复选框设置为未选中

[英]How can I set a checkbox to unchecked when I click cancel

I have a bootbox script that displays a custom alert. 我有一个显示自定义警报的引导脚本。 When clicking cancel I need the checkbox that fires the alert to go back to unchecked. 单击“取消”时,我需要触发警报的复选框以恢复为未选中状态。

This is my checkbox when checked 选中时这是我的复选框

<div class="checkbox">
     <label>
     <input type="checkbox" id="enroll-deposit" checked="" data-original-title="" title="" value="True"> I am not enrolling in Direct Deposit
     </label>
</div>

and this is my bootbox script thats fired on click 这是我在点击时触发的启动箱脚本

$(document).on("click", "#enroll-deposit", function(e) {
    var val = $(this).val();
    if (val == "True") {
        var url = "@Url.Action("waivetask", "OBProcess" , new { id=ViewBag.DocId, tid=ViewBag.TaskId })";
        var desc = "Direct Deposit";
        bootbox.dialog({
            message: "Are you sure you want to waive " + "<strong>" + desc + "</strong>",
            title: "Waive Direct Deposit",
            buttons: {
                main: {
                    label: "Cancel",
                    className: "btn-default",
                    callback: function() {
                        //NOT SURE WHAT TO PUT
                    }
                },
                danger: {
                    label: "Waive and Move On to Next Task",
                    className: "btn-danger",
                    callback: function() {
                        window.location.href = url;
                    }
                }
            }
        });
    }
});

I am not sure what to do when the user clicks the cancel button. 我不确定当用户单击取消按钮时该怎么办。 I want to uncheck the enroll-deposit box. 我要取消选中“注册存款”框。

There is another way to uncheck: 还有另一种取消选中的方法:

$('input[type=checkbox]').prop('checked', false);

And instead of using: 而不是使用:

var val = $(this).val();

You could use: 您可以使用:

var isChecked = $(this).is(':checked');  

Maybe its best to refactor your code like this: 也许最好像这样重构您的代码:

$(document).on("click", "#enroll-deposit", function() {
    var isChecked = $(this).is(':checked');    
    if (isChecked) {
        var url = '@Url.Action("waivetask", "OBProcess" , new { id=ViewBag.DocId, tid=ViewBag.TaskId })';
        bootbox.dialog({
            message: "Are you sure you want to waive <strong> Direct Deposit </strong>",
            title: "Waive Direct Deposit",
            buttons: {
                main: {
                    label: "Cancel",
                    className: "btn-default",
                    callback: function() {
                        //NOT SURE WHAT TO PUT
                    }
                },
                danger: {
                    label: "Waive and Move On to Next Task",
                    className: "btn-danger",
                    callback: function() {
                        window.location.href = url;
                    }
                }
            }
        });
    }
});

Hope I was helpful. 希望我会有所帮助。

$('input:checkbox').removeAttr('checked');

写这个

$("#enroll-deposit").prop('checked',false);

Checked is not an attribute. 选中不是属性。 It's actually a property. 它实际上是一个财产。 Looks like you're using jQuery so in your "else" statement you could do 看起来您正在使用jQuery,因此可以在“ else”语句中执行

$(this).prop("checked",false);

暂无
暂无

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

相关问题 如何提交未选中复选框的值 - how can I submit the value of an unchecked checkbox 当复选框被选中或复选框未选中删除值时,如何在另一个文本框中获得复选框值和文本框值的总和 - how can i get sum of checkbox value and textbox value in another textbox when checkbox is checked or if checkbox unchecked remove the value 选中或未选中时,如何使用复选框在选项卡之间切换? - How can I use a checkbox to switch between tabs when checked or unchecked? 如何将未选中的复选框传递给jQuery中的cookie? - How can I pass unchecked checkbox to cookie in jQuery? 我可以使用一个功能来选中或取消选中所有复选框(不使用点击触发器)吗? - Can I use a function to let all checkbox checked or unchecked(not use click trigger)? 单击复选框时如何禁用页面重定向? - How I can disable page redirect when click on checkbox? 如何在未选中复选框时显示Javascript确认框,然后如果用户选择取消,请选中复选框? - How to show Javascript confirm box when checkbox is unchecked and then if user selects cancel, leave the checkbox checked? 取消选中复选框时设置复选框值 - Set checkbox value when checkbox is unchecked 如何在单击父复选框时取消选中子复选框 - How to unchecked child checkbox on click parent checkbox 如何为选中和未选中的复选框设置事件处理程序? - How could I set up an event handler for my checkbox for checked and unchecked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM