[英]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.