admin管理员组

文章数量:1277502

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 this?

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 this?

Share Improve this question edited Dec 7, 2022 at 16:54 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 17, 2011 at 21:44 Justin MeltzerJustin Meltzer 13.6k34 gold badges119 silver badges182 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

Just attach a change event to it:

$('#video_is_derivative_true').change(function(){
 console.log("Selected");   
})

example: http://jsfiddle/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 bees 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"); }); 

本文标签: javascriptTrigger some jQuery code when a radio button is selectedStack Overflow