admin管理员组文章数量:1430529
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
1 Answer
Reset to default 4Always 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");
}
});
本文标签: javascriptJQuery hidden checkbox checkingbut not uncheckingStack Overflow
版权声明:本文标题:javascript - JQuery hidden checkbox checking, but not unchecking - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745554275a2663087.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论