bootstrap-javascript:modal i复选框在关闭时未取消选中

bootstrap-javascript:modal i复选框在关闭时未取消选中,javascript,bootstrap-4,Javascript,Bootstrap 4,我有下面的html <div class="modal inmodal" id="handleUserModal" tabindex="-1" role="dialog" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-conten

我有下面的html

<div class="modal inmodal" id="handleUserModal" tabindex="-1" role="dialog"  aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content animated fadeIn">
            <form class="form">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title">Add user</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group row profile_field">
                        <label class="col-lg-3 col-form-label">First Name</label>
                        <div class="col-lg-9">
                            <input id="firstname" name="firstname" type="text" placeholder="First Name" class="form-control">
                        </div>
                    </div>
                    <div class="form-group row reset_password_field mb-0 mt-4">
                        <div class="col-lg-offset-2 col-lg-10">
                            <div class="i-checks reset_password_check" id="password_check">
                                <label> <input id="passresetcheck" type="checkbox" /> Reset password </label>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" id="saveButton">Save</button>
                </div>
            </form>
        </div>
    </div>
</div>

使用模态id
如果你抓到了什么东西,一定要先检查一下。在您的情况下,
$('passresetcheck')
是未定义的

您需要使用#符号(如此处的
$(“#passresetcheck”)
按ID进行搜索:

const checkbox=$(“#passresetcheck”)
checkbox.prop('checked',false)
setTimeout(()=>checkbox.prop('checked',true),2000)

&时代;接近
添加用户
名字
重置密码
接近
拯救

始终首先检查是否捕获了某些内容。在您的情况下,
$('passresetcheck')
是未定义的

您需要使用#符号(如此处的
$(“#passresetcheck”)
按ID进行搜索:

const checkbox=$(“#passresetcheck”)
checkbox.prop('checked',false)
setTimeout(()=>checkbox.prop('checked',true),2000)

&时代;接近
添加用户
名字
重置密码
接近
拯救

最后,对我有效的方法是删除选中的类

// this is for the input
$('#passresetcheck').prop('checked', false);

// this is to remove checked class
$("#password_check div").each(function(index,elem){
  if (elem.classList.contains("checked")) {
    elem.classList.remove("checked");
  }
})  
我的观察结果是,当我们检查html时会发生什么

这是没有任何检查的


<div class="form-group row reset_password_field mb-0 mt-4">
    <div class="col-lg-offset-2 col-lg-10">
        <div class="i-checks reset_password_check" id="password_check">
            <label> <input id="passresetcheck" type="checkbox" /> Reset password </label>
        </div>
    </div>
</div>

重置密码
检查后,它会像这样进行转换,并在i-checks中的div中添加一个已检查的类


<div class="i-checks reset_password_check" id="password_check">
    <label class=""> 
        <div class="icheckbox_square-green checked" style="position: relative;">
            <input id="passresetcheck" type="checkbox" style="position: absolute; opacity: 0;">
            <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;"></ins>
        </div>
        Reset password 
    </label>
</div>

重置密码

最后,对我有效的方法是删除选中的类

// this is for the input
$('#passresetcheck').prop('checked', false);

// this is to remove checked class
$("#password_check div").each(function(index,elem){
  if (elem.classList.contains("checked")) {
    elem.classList.remove("checked");
  }
})  
我的观察结果是,当我们检查html时会发生什么

这是没有任何检查的


<div class="form-group row reset_password_field mb-0 mt-4">
    <div class="col-lg-offset-2 col-lg-10">
        <div class="i-checks reset_password_check" id="password_check">
            <label> <input id="passresetcheck" type="checkbox" /> Reset password </label>
        </div>
    </div>
</div>

重置密码
检查后,它会像这样进行转换,并在i-checks中的div中添加一个已检查的类


<div class="i-checks reset_password_check" id="password_check">
    <label class=""> 
        <div class="icheckbox_square-green checked" style="position: relative;">
            <input id="passresetcheck" type="checkbox" style="position: absolute; opacity: 0;">
            <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;"></ins>
        </div>
        Reset password 
    </label>
</div>

重置密码

如果你在使用jQuery,为什么不直接使用.removeClass()函数?如果你在使用jQuery,为什么不直接使用.removeClass()函数?嘿@santhosh yedidi,请接受并投票表决我的答案,如果它有帮助的话。嘿@santhosh yedidi,请接受并投票表决我的答案