获取 jquery ui 按钮集中当前选中的单选按钮,无需绑定点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8908943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:14:30  来源:igfitidea点击:

Get the currently selected radio button in a jquery ui buttonset without binding to click

jqueryjquery-ui

提问by Dave

I have radio buttons like this:

我有这样的单选按钮:

<div id="radio">
    <input type="radio" id="radio1" name="radioOption" /><label for="radio1">Choice 1</label>
    <input type="radio" id="radio2" name="radioOption" checked="checked" /><label for="radio2">Choice 2</label>
    <input type="radio" id="radio3" name="radioOption" /><label for="radio3">Choice 3</label>
</div>

And apply the button set like this:

并应用这样的按钮集:

$(function () {
    $("#radio").buttonset();
});

I need to get the selected radio button without binding to the click event.

我需要在不绑定到单击事件的情况下获取选定的单选按钮。

回答by Dave

Damn just realised my selecter had a typo, but if it helps anyone else:

该死的刚刚意识到我的选择器有一个错字,但如果它对其他人有帮助:

If you dont want to use the click event, which I didn't you could do the following: If you're looking for the element it self use:

如果您不想使用我没有的点击事件,您可以执行以下操作:如果您正在寻找它自己使用的元素:

$("#radio :radio:checked");

If you're looking for the id of the element use:

如果您正在寻找元素的 id,请使用:

$("#radio :radio:checked").attr('id');

If you're need to know what the text of the value selected then this should work:

如果您需要知道所选值的文本是什么,那么这应该有效:

$("#radio :radio:checked + label").text();

回答by Sameera Thilakasiri

Try this

尝试这个

$("input:radio[name=radioOption]").click(function(){
      var value = $(this).attr("id");
      alert(value);
})

Example

例子

回答by Umesh Patil

As I see, there is not such event for buttonset on jquery ui. You can see here.

如我所见,jquery ui 上的按钮集没有这样的事件。你可以在这里看到。

You need to write manual jquery bind like below:

您需要编写手动 jquery 绑定,如下所示:

<script type="text/javascript">
    $(document).ready(function() {
        $( "#radio" ).buttonset();
        $("#radio input").bind("click",function(){
               // do whatever you want to do with clicked button
    });
});
</script>

回答by user859551

Just add the valueattribute to every inputas follows:

只需将value属性添加到 each input,如下所示:

<div id="radio">         
    <input type="radio" id="radio1" name="radioOption" checked="checked" value="first"/><label for="radio2">Choice 2</label>
    <input type="radio" id="radio2" name="radioOption" value="second"/><label for="radio3">Choice 3</label>
    <input type="radio" id="radio3" name="radioOption" value="third"/><label for="radio3">Choice 3</label>
</div>

回答by Jose de Soto

When I click in a button I call one function, it takes parameters from the buttonSet:

当我单击一个按钮时,我调用了一个函数,它从 buttonSet 中获取参数:

$("#button").click( function()
{

var params = $("input[name=radio]:checked").val();
alert(params);

});

In your case if you do this, I think it should work:

在你的情况下,如果你这样做,我认为它应该有效:

var params = $("input[name=radioOption]:checked").val();