How to reset radiobuttons in jQuery so that none is checked

后端 未结 13 836
一个人的身影
一个人的身影 2020-11-29 16:35

I have radio buttons in HTML like this:


    1
    

        
13条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 17:30

    Finally after a lot of tests, I think the most convenient and efficient way to preset is:

    var presetValue = "black";
    $("input[name=correctAnswer]").filter("[value=" + presetValue + "]").prop("checked",true);
    $("input[name=correctAnswer]").button( "refresh" );//JQuery UI only
    

    The refresh is required with the JQueryUI object.

    Retrieving the value is easy :

    alert($('input[name=correctAnswer]:checked').val())
    

    Tested with JQuery 1.6.1, JQuery UI 1.8.

提交回复
热议问题