.checked 不会改变 属性 css 状态

.checked doesn't change property css state

看来我的简单检查 属性 切换不能正常工作,我的功能如下:

$('#enable_payment').click(function (e) {
            e.preventDefault();
            var form = $('.payment-system-form');
            if(form.hasClass('show-fees')){
                swal({
                    title: "Are you sure?",
                    text: "Transactions made outside........",
                    type: "error",
                    confirmButtonClass: "btn-danger",
                    confirmButtonText: "Yes!",
                    showCancelButton: true
                }, function() {
                    //on okay
                    $("input[name='enable_payment']")[0].checked = false;
                    form.removeClass('show-fees');
                }, function() {
                    //on cancel
                });
            } else {
                $("input[name='enable_payment']")[0].checked = true;
                form.addClass('show-fees');
            }
        });

最初在页面加载时,$("input[name='enable_payment']")[0].checked 设置为 true,就像这样 <input type='checkbox' name='enable_payment' checked>

然后我想在它即将被切换为禁用后添加一个警报,至于我需要添加 e.preventDefault() 一旦我添加了这个我就无法再切换 checked 属性?

我都试过了

$("input[name='enable_payment']").prop('checked', true);
$("input[name='enable_payment']").prop('checked');

但是,如果我 运行 $("input[name='enable_payment']").prop('checked', true); 在我的 Google Chrome 开发者工具中,它会按预期工作吗?

所以总而言之,我的问题是 $("input[name='enable_payment']").checked 始终保持 false 并且无法再次 true在我的 swal() 函数

上单击 okay

这是 jsFiddle:https://jsfiddle.net/vcz48umx/

它不起作用,因为点击事件是在自定义检查器输入类型复选框的父元素上触发的,因此您只需将点击功能更改为它的父元素

//parent() method
$('#enable_payment').parent().click(function (e)