开发者

use an href to click a checkbox to fire an event

开发者 https://www.devze.com 2023-03-16 18:53 出处:网络
I have some code that looks like this: $(\'#clearSelections\').click(function(e) { $(\"#isAcheckbox\").removeAttr(\"checked\");

I have some code that looks like this:

$('#clearSelections').click(function(e) {
    $("#isAcheckbox").removeAttr("checked");
    redrawTableofListings();
});     


<a href="#" id="clearSelections">clear selections</a>

<input type="checkbox" id="isAcheckbox" checked>

// when the user clicks on the checkbox, doSomeThing happens.

basically, when the user clicks on the href, i want to use jquery to make the href handle the checkbox the same way as though the user just put a check in the checkbox.

Does this make sense? When I use "r开发者_运维问答emoveAttr" it just removes the checkbox, and doesen't redraw the form.

Thanks in advance! and be safe with fireworks this weekend!


It seems you always want to deselect the checkbox when the link is clicked (at least this is what I infer by the text clear selections). Then this will do it:

$('#clearSelections').click(function(e) {
    e.preventDefault(); // don't follow the link
    $("#isAcheckbox").prop("checked", false);
    redrawTableofListings();
});

DEMO


Use

$("#isAcheckbox").trigger('click');
0

精彩评论

暂无评论...
验证码 换一张
取 消