Trigger some jQuery code when a radio button is selected
I have these two radio buttons:
Original <input id="video_is_derivative_false" name="video[is_derivative]" type="radio" value="false">
Derivative (<i>ex. remix, mashup etc...</i>) <input id="video_is_derivative_true" name="video[is_derivative]" type="radio" value="true">
and I want to call some jQuery code when the "Derivative" button is selected. How can I do t开发者_如何转开发his?
Just attach a change event to it:
$('#video_is_derivative_true').change(function(){
console.log("Selected");
})
example: http://jsfiddle.net/niklasvh/cyADB/
$("#video_is_derivative_true").click(function(){
alert("your code goes here");
});
Add an onclick handler to the input tag
You could also put something on the change handler
$("#video_is_derivative_true").change(function(){
if($(this).is(':checked')){
alert("more code here");
}
});
You will want to monitor the change event, then in the handler, check to make sure that the button is checked. The second part is important because the change event will also fire when it becomes unchecked. The code would look something like this:
$('#video_is_derivative_true').change(function() {
if (this.checked) {
alert('derivative checked!');
}
});
Here's a live demo ->
$('#video_is_derivative_true').bind('click change', function() {
if (this.checked) {
// derivative is checked
}
});
$("#video_is_derivative_true").click(function(){ alert("your code goes here"); });
精彩评论