jquery javascript: take action when radio button is selected
I have the following radio buttons, none of which is checked by default.
<input type="radio" name="location" value="0" /> home
<input type="radio" name="location" value="1" /> work
<input t开发者_开发知识库ype="radio" name="location" value="2" /> school
How do I detect when either of them is checked. I'm looking for something like click, but it's not working
$("input[name=location]").click(function(){
alert("selected");
});
$('input[name=location]').change(function(){
alert(this.checked);
});
Try surrounding the code with below lines
$(document).ready(function(){
}
);
Have you treid the onChange event?
$("input[name=location]").bind('change', function(){
alert("selected");
});
This works fine for me. Are you referencing well the jquery library?
$(document).ready(function(){
$("input[name=location]").bind('change', function(){
alert("Hello!");
});
});
精彩评论