How can I simulate a click on a jQuery UI radio button?

元气小坏坏 提交于 2019-12-05 14:25:15

问题


I have some radio buttons

<div id="typeRadios">
    <input id="character_chartype_a" name="character[chartype]" type="radio" value="A" /><label for="character_chartype_a">A</label>
    <input id="character_chartype_a" name="character[chartype]" type="radio" value="B" /><label for="character_chartype_b">B</label>
</div>

that I turn into jQuery UI buttons

$("#typeRadios").buttonset();

What line of code can I use to simulate a click on one of the buttons? I've tried this

// here data.chartype equals "A"
$("input[value='"+data.chartype+"']").click();

but it doesn't work. Thanks for reading.


回答1:


You have to do it with the label element added by jQuery UI. Try:

$("label[for='character_chartype_"+data.chartype+"']").click();

Have a look at it here, in a controlled environment: http://jsfiddle.net/B3d4z/




回答2:


I believe you're looking for the select event.

$("input[value='"+data.chartype+"']").select();



回答3:


<div class="radioVote">
<input type="radio" id="radio1" name="radio" value="1"/><label for="radio1">ONE</label>
<input type="radio" id="radio2" name="radio" value="2"/><label for="radio2">TWO</label>
</div> 



$(document).ready(function(){

        $( ".radioVote" ).buttonset();

        $('[for^=radio]').click(function() {
            var theRadioElement = $(this).prev();
            alert("Button" + theRadioElement.attr('id') + " clicked. VALUE:" + theRadioElement.val());
        });

    });

This example shows how to get the ID and Value when you click on a label that belongs to a jQueryUI radio.

Happy coding :)



来源:https://stackoverflow.com/questions/3593301/how-can-i-simulate-a-click-on-a-jquery-ui-radio-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!