jQuery Jquery通过测试值获取单选按钮的ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16681390/
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-26 17:35:10  来源:igfitidea点击:

Jquery get id of radio button by testing value

jqueryradio-button

提问by manisha

I have a read only form that I want to populate the values returned from an ajax query. What I don't understand is the correct syntax to get the id of a specific radio button. I know what value exists in the db, but how to get the id of the radio button, to change the radio button attribute?

我有一个只读表单,我想填充从 ajax 查询返回的值。我不明白的是获取特定单选按钮 ID 的正确语法。我知道数据库中存在什么值,但是如何获取单选按钮的 id 以更改单选按钮属性?

For example, in the Radio Button options with the "name"==Criteria1Score", how to I return the id of the input where value ==Good.

例如,在带有“name”==Criteria1Score”的单选按钮选项中,如何返回输入的id,其中值==Good。

Here is an example of the form structure:

这是表单结构的示例:

<legend>Criteria1</legend>
<label class="radio inline">
    Great
    <input id="Criteria1Score1" class="Score" name="Criteria1Score" type="radio" value="Great">
</label>
<label class="radio inline">
    Good
    <input id="Criteria1Score2" class="Score" name="Criteria1Score" type="radio" value="Good">
</label>
<label class="radio inline">
    Bad
    <input id="Criteria1Score3" class="Score" name="Criteria1Score" type="radio" value="Bad">
</label>

回答by km6zla

$('input[type=radio][name=Criteria1Score]:checked').attr('id')

$('input[type=radio][name=Criteria1Score]:checked').attr('id')

Working Demo:

工作演示:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<legend>Criteria1</legend>
<label class="radio inline">
    Great
    <input id="Criteria1Score1" class="Score" name="Criteria1Score" type="radio" value="Great"/>
</label>
<label class="radio inline">
    Good
    <input id="Criteria1Score2" class="Score" name="Criteria1Score" type="radio" value="Good"/>
</label>
<label class="radio inline">
    Bad
    <input id="Criteria1Score3" class="Score" name="Criteria1Score" type="radio" value="Bad"/>
</label>

<input type="button" onclick="alert($('input[type=radio][name=Criteria1Score]:checked').attr('id'))" value="click me to get id of checked input" />

JSFiddle

JSFiddle

回答by tymeJV

You can loop through those inputs to do what you need:

您可以遍历这些输入以执行您需要的操作:

$("input[name='Criteria1Score']").each(function() {
    if (this.value == "Good") {
        //do something with this input
    }
});

回答by Robin Manoli

$("input[value='Good']").attr("id");

If you want to select the input by its value

如果要按其值选择输入

回答by aug

You can try

你可以试试

var selected_Id = $('input[name="Criteria1Score"]:selected').attr('id');

EDIT: Sorry didn't see the second part. If you want to check the value == Good you can do

编辑:抱歉没有看到第二部分。如果你想检查价值 == 好你可以做

if ($('input[name="Criteria1Score"]:selected').val() == 'Good') {
    //do something
}

回答by madhu reddy

I got it by using checked.

我是通过使用检查得到的。

var selected_Id = $('input[name="Criteria1Score"]:checked').attr('id');

if ($('input[name="Criteria1Score"]:checked').val() == 'Good') {
    //do something
}

回答by beatcode

Jquery get id of radio button by testing value,

Jquery 通过测试值获取单选按钮的 id

<legend>You are ready for</legend> 
<label class="radio inline">
   Success
    <input id="success" class="win" name="successvalue" type="radio" value="Great"/>
</label>
<label class="radio inline">
    Big Success
    <input id="bigsuccess" class="win" name="successvalue" type="radio" value="Good"/>
</label>
<br/>

<input type="button" onclick="alert($('input[name=successvalue]:checked').attr('id'))" value="click me too" />

See fiddle

小提琴

Worked as code to get id value on checked radio button

用作代码以在选中的单选按钮上获取 id 值