最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery hidden checkbox checking, but not unchecking - Stack Overflow

programmeradmin1浏览0评论

I have created a replacement img checkbox for the site I am working on. I can successfully get the checkbox to check, but not to uncheck. I can see this change in Inspect.

What is wrong with the code? There are no errors in the page. Thanks!

HTML is:

<input type="checkbox" id="AcceptTerms_check" name="AcceptTerms_check" style="display:none;">
<img class="chk-img" id="AcceptTerms" onclick="CheckboxClick(this);" src="../../../wp-content/uploads/misc/notselected.png">
<span id="ts-cs-accept">I have read and accepted the terms</span>.

JS is:

function CheckboxClick(element) {
    if(jQuery('input:checkbox[name=' + element.id + '_check]').is(":checked")) {
        jQuery('input:checkbox[name=' + element.id + '_check]').attr('checked','false');
        jQuery(element).attr("src", "../../../wp-content/uploads/misc/notselected.png");
    } else {
        jQuery('input:checkbox[name=' + element.id + '_check]').attr('checked','true');
        jQuery(element).attr("src", "../../../wp-content/uploads/misc/selected.png");
    };
};

I have created a replacement img checkbox for the site I am working on. I can successfully get the checkbox to check, but not to uncheck. I can see this change in Inspect.

What is wrong with the code? There are no errors in the page. Thanks!

HTML is:

<input type="checkbox" id="AcceptTerms_check" name="AcceptTerms_check" style="display:none;">
<img class="chk-img" id="AcceptTerms" onclick="CheckboxClick(this);" src="../../../wp-content/uploads/misc/notselected.png">
<span id="ts-cs-accept">I have read and accepted the terms</span>.

JS is:

function CheckboxClick(element) {
    if(jQuery('input:checkbox[name=' + element.id + '_check]').is(":checked")) {
        jQuery('input:checkbox[name=' + element.id + '_check]').attr('checked','false');
        jQuery(element).attr("src", "../../../wp-content/uploads/misc/notselected.png");
    } else {
        jQuery('input:checkbox[name=' + element.id + '_check]').attr('checked','true');
        jQuery(element).attr("src", "../../../wp-content/uploads/misc/selected.png");
    };
};
Share Improve this question asked Oct 13, 2014 at 13:26 TheRealPapaTheRealPapa 4,5399 gold badges80 silver badges165 bronze badges 1
  • 7 Use .prop() instead of .attr() – Satpal Commented Oct 13, 2014 at 13:27
Add a ment  | 

1 Answer 1

Reset to default 4

Always use prop with checkboxes, as you can use true and false values directly. You can also reduce your code a lot and not repeat plex jQuery selectors:

function CheckboxClick(element) {
    var $checkbox = jQuery('input:checkbox[name=' + element.id + '_check]')
    if($checkbox.prop("checked")) {
        $checkbox.prop('checked', false);
        jQuery(element).attr("src", "../../../wp-content/uploads/misc/notselected.png");
    } else {
        $checkbox.prop('checked', true);
        jQuery(element).attr("src", "../../../wp-content/uploads/misc/selected.png");
    };
};

This can be simplified further, but I wanted you to still recognize your code :)

e.g. you can toggle it with:

$checkbox.prop("checked", !$checkbox.prop("checked"));

Update... Do not use attribute based events with jQuery (e.g. onclick=), use the jQuery way:

Try this way instead: http://jsfiddle/noy45sjg/1/

jQuery('.chk-img').click(function (e) {
    var element = jQuery(this);
    var $checkbox = jQuery('input:checkbox[name=' + this.id + '_check]');
    if($checkbox.prop("checked")) {
        $checkbox.prop('checked', false);
        jQuery(element).attr("src", "../../../wp-content/uploads/misc/notselected.png");
    } else {
        $checkbox.prop('checked', true);
        jQuery(element).attr("src", "../../../wp-content/uploads/misc/selected.png");
    }
});
发布评论

评论列表(0)

  1. 暂无评论