Simple checkbox not firing the jQuery code when clicked
on this page, I made a checkbox: http://www.problemio.com
The checkbox looks like this:
<input class="problem_follow_checkbox" TYPE="checkbox" NAME="follow_problem" /> Follow t开发者_如何学运维his problem
And in the jQuery I added this:
$("#problem_follow_checkbox").click (function ()
{
alert ("clicked cvheckbox for following problem");
var thisCheck = $(this);
if (thischeck.is (':checked'))
{
// Do stuff
alert ("yes checjed");
}
else
{
alert ("not checjed");
}
});
Seems super straight forward, but for some reason when the checkbox is checked, this jQuery code does not get invoked. Any reason that might be happening?
Thanks!
Use:
$(".problem_follow_checkbox").click (function ()
{
alert ("clicked cvheckbox for following problem");
var thisCheck = $(this);
if (thisCheck.is (':checked'))
{
// Do stuff
alert ("yes checjed");
}
else
{
alert ("not checjed");
}
});
$("#problem_follow_checkbox") is looking for the ID problem_follow_checkbox so $(".problem_follow_checkbox") will be working nice.
You need to use selector starting with "." if you're selecting by "class" attributed. "#" prefix is used for selecting by id. So, either do, as suggested above:
$(".problem_follow_checkbox").click( ....
or change your input element to something like:
<input id="problem_follow_checkbox"
and then select by
$("#problem_follow_checkbox").click (
精彩评论