简体   繁体   English

.checked不会更改属性CSS状态

[英].checked doesn't change property css state

It appears as though my simple checked property toggle doesn't want to work correctly, my function is as follows: 似乎我的简单选中属性切换器不想正常工作,我的功能如下:

$('#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');
            }
        });

Initially on page load, $("input[name='enable_payment']")[0].checked is set to true like so <input type='checkbox' name='enable_payment' checked> 最初在页面加载时, $("input[name='enable_payment']")[0].checked设置为true ,因此<input type='checkbox' name='enable_payment' checked>

I then wanted to add an alert after it was about to be toggled to disabled, as to which I needed to add the e.preventDefault() once I added this I could no longer toggle the the checked property? 然后,我想在警报将要切换为禁用后添加一个警报,因为添加该警报后​​我需要添加e.preventDefault() ,因此我无法再切换checked属性了吗?

I've tried doing both 我都尝试过

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

But, if I run $("input[name='enable_payment']").prop('checked', true); 但是,如果我运行$("input[name='enable_payment']").prop('checked', true); within my Google Chrome Developer Tools it works as expected? 在我的Google Chrome开发者工具中可以正常工作?

So all in all, my problem is that $("input[name='enable_payment']").checked always remains false & can't be made true again once I've clicked the okay on my swal() function 因此,所有的一切,我的问题是, $("input[name='enable_payment']").checked始终保持一次,我点击了还好我无法进行再 swal()函数

Here's the jsFiddle: https://jsfiddle.net/vcz48umx/ 这是jsFiddle: https ://jsfiddle.net/vcz48umx/

Its not working because the click event is firing on parent element of input type checkbox which is custom checker so you just need to change click function to its parent 它不起作用,因为click事件正在自定义检查器的输入类型复选框的父元素上触发,因此您只需将click函数更改为其父元素

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM