简体   繁体   English

如何模拟jQuery UI单选按钮上的单击?

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

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 我转向jQuery UI按钮

$("#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. 谢谢阅读。

You have to do it with the label element added by jQuery UI. 你必须使用jQuery UI添加的label元素。 Try: 尝试:

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

Have a look at it here, in a controlled environment: http://jsfiddle.net/B3d4z/ 在这里,在受控环境中查看它: http//jsfiddle.net/B3d4z/

我相信你正在寻找select活动。

$("input[value='"+data.chartype+"']").select();
<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. 此示例显示如何在单击属于jQueryUI无线电的标签时获取ID和值。

Happy coding :) 快乐编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM