Answer the question
In order to leave comments, you need to log in
jQuery - Programmatically triggering a click() event?
There is a hierarchical list of categories, presented as an unordered UL-LI list with checkboxes. This kind:
<ul><br>
<li><input type="checkbox" value="1" rel="c0" id="c1" class="category"/><label for="c1">Электроника</label></li><br>
<ul><br>
<li><input type="checkbox" value="2" rel="c1" id="c2" class="category"/><label for="c2">Телефоны</label></li><br>
<ul><br>
<li><input type="checkbox" value="3" rel="c2" id="c3" class="category"/><label for="c3">Сотовые телефоны</label></li><br>
</ul><br>
</ul><br>
</ul><br>
$(document).ready(function() {<br>
$('.category').bind('click', function() {<br>
var category_id = $(this).val();<br>
var parent = $(this).attr('rel');<br>
<br>
if($(this).attr('checked')) {<br>
window.categories.push = category_id;<br>
}<br>
<br>
if(parent != 'c0') $('#'+parent).click();<br>
}); <br>
})<br>
$(this).attr('checked')
somehow returns false , while the mark is actually being made; and vice versa, returns true , while the check is actually unchecked. Answer the question
In order to leave comments, you need to log in
You need to check on the onChange event, since it occurs after a click.
$(".checkbox").bind("change",function(){
if ($(this).is(":checked")) {
// true
}
})
First, your html is not correct, ul cannot be nested in ul. You need to nest ul in li
Secondly, if you click on the label, and not on the checkbox, then the checkbox will be set, but click will not be called.
In general, I see two solutions:
1. If you want to use checkboxes: hang a handler on change, and then go through the parents (when you fix the html, it will be easier to do this) and put down the checked
2 attribute everywhere. If you want to use click, you will have to abandon the checkbox and use some kind of styling (for example, draw a picture of a check mark to the left of the text, and put a mark in a hidden field ), and hang the click handler on li. In this case, you do not have to use any program calls to click, but will manage with the usual bubbling, i.e. the click event itself will be called on all parent elements in turn (again, for this you will have to normalize the html)
To all the above comments: it's better to use $().prop('checked', true || false) rather than attr().
This explanation immediately suggests itself - when you programmatically call a click on a checkbox, you only call the corresponding handler, without changing the value of the checked attribute, which you access via $(this).attr('checked').
Don't trigger a click, put the attribute $('#c2').attr('checked', true);
First, we need to fix the html code. Nested uls must be inside li. Javascript would be something like this:
$('input').change(function() {
if($(this).is(':checked'))
$(this).parents('li').find('> input').removeAttr('checked');
else
$(this).parents('li').find('> input').attr('checked', 'checked');
});
and how to click on the radiobutton in jquery if it is styled and the check is in the background?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question