最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using jQuery, what is the best way to set onClick event listeners for radio buttons? - Stack Overflow

programmeradmin9浏览0评论

For the following HTML:

<form name="myForm">
    <label>One<input  name="area"  type="radio" value="S"  /></label>
    <label>Two<input name="area"   type="radio" value="R" /></label>
    <label>Three<input name="area"   type="radio" value="O" /></label>
    <label>Four<input name="area" type="radio" value="U" /></label>
</form>

Changing from the following JavaScript code:

$(function() {
     var myForm = document.myForm;
     var radios = myForm.area;
     
     // Loop through radio buttons
     for (var i=0; i<radios.length; i++) {
        if (radios[i].value == "S") {
            radios[i].checked = true;   // Selected when form displays
            radioClicks();   // Execute the function, initial setup
        }
        radios[i].onclick = radioClicks;  // Assign to run when clicked
     }  
 });

Thanks

EDIT: The response I selected answers the question I asked, however I like the answer that uses bind() because it also shows how to distinguish the group of radio buttons

For the following HTML:

<form name="myForm">
    <label>One<input  name="area"  type="radio" value="S"  /></label>
    <label>Two<input name="area"   type="radio" value="R" /></label>
    <label>Three<input name="area"   type="radio" value="O" /></label>
    <label>Four<input name="area" type="radio" value="U" /></label>
</form>

Changing from the following JavaScript code:

$(function() {
     var myForm = document.myForm;
     var radios = myForm.area;
     
     // Loop through radio buttons
     for (var i=0; i<radios.length; i++) {
        if (radios[i].value == "S") {
            radios[i].checked = true;   // Selected when form displays
            radioClicks();   // Execute the function, initial setup
        }
        radios[i].onclick = radioClicks;  // Assign to run when clicked
     }  
 });

Thanks

EDIT: The response I selected answers the question I asked, however I like the answer that uses bind() because it also shows how to distinguish the group of radio buttons

Share Improve this question edited Aug 12, 2020 at 12:47 Pablo Martinez 2,1821 gold badge24 silver badges29 bronze badges asked Sep 5, 2008 at 19:35 Jay CorbettJay Corbett 28.4k21 gold badges58 silver badges74 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 19
$(document).ready(function(){
    $("input[name='area']").bind("click", radioClicks);
});

functionradioClicks() {
    alert($(this).val());
}

I like to use bind() instead of directly wiring the event handler because you can pass additional data to the event hander (not shown here but the data is a third bind() argument) and because you can easily unbind it (and you can bind and unbind by group--see the jQuery docs).

http://docs.jquery.com/Events/bind#typedatafn

$( function() {
    $("input:radio")
        .click(radioClicks)
        .filter("[value='S']")
        .attr("checked", "checked");
});
$(function() {

  $("form#myForm input[type='radio']").click( fn );

});

function fn()
{
   //do stuff here
}
$(function() {
    $('input[@type="radio"]').click(radioClicks);
});

I think something like this should work (but it's untested):

$("input[@type='radio']").each(function(i) {
    if (this.val() == 'E') {
        radioClicks();
        this.get().checked = true;
    }
}
$("input[@type='radio']").click(radioClicks);
$(function() {
    $('#myForm :radio').each(function() {
        if ($(this).value == 'S') {
            $(this).attr("checked", true);
            radioClicks();
        }

        $(this).click(radioClicks);
    });
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论