简体   繁体   English

使用jquery获取选定的单选按钮

[英]get the selected radio button using jquery

I have a form which looks like this: 我有一个看起来像这样的表格:

<input type="radio" id="answer0" name="answer0" value="1" />
<input type="radio" id="answer0" name="answer0" value="2" />
<input type="radio" id="answer0" name="answer0" value="3" />
<input type="radio" id="answer0" name="answer0" value="4" />

What I want is to get the selected radio button so I used the jQuery code below. 我想要的是获取所选的单选按钮,所以我使用下面的jQuery代码。

var id = "#answer0";

var isChecked = $(id).prop('checked');

But I only get a result when the first radio button is checked. 但是我只在选中第一个单选按钮时得到结果。 No result if I select the second, third and fourth one. 如果我选择第二个,第三个和第四个,那就没有结果。 Any help please. 请帮忙。

Oh sorry. 哦对不起。 I generated the radio button in php like this... 我像这样在php中生成单选按钮...

print '<input type="radio" class="answer'. $q . '" id="answer'. $q . '" name="answer'. $q . '"value="1"/>' . $row['answer1'] .'<br/>';
print '<input type="radio" class="answer'. $q . '" id="answer'. $q . '" name="answer'. $q . '"value="2"/>' . $row['answer2'] .'<br/>';
print '<input type="radio" class="answer'. $q . '" id="answer'. $q . '" name="answer'. $q . '"value="3"/>' . $row['answer3'] .'<br/>';
print '<input type="radio" class="answer'. $q . '" id="answer'. $q . '" name="answer'. $q . '"value="4"/>' . $row['answer4'] .'<br/>';

so I'm sure that the id is unique. 所以我确定id是唯一的。 What I am trying to do is get the selected radio button value for answer0, answer1 and so on... So far I can only get the first group value (answer0). 我要做的是获取answer0,answer1的所选单选按钮值等等......到目前为止,我只能获得第一个组值(answer0)。

my jquery code: 我的jquery代码:

for ( var i = 1; i <=items; i++ ) {
    var t = "answer" + i;

    if($('.' + t).prop('checked')){
        //alert(this.id);
        alert(t);
        correct++;
    }

    alert(correct);

    }}

ID of element has to be unique. 元素的ID必须是唯一的。

Give those inputs a class name , for example .radio. 为这些输入提供类名,例如.radio。

<input type="radio" id="answer0" class='radio' name="answer0" value="1" />
<input type="radio" id="answer1" class='radio' name="answer0" value="2" />
<input type="radio" id="answer2" class='radio' name="answer0" value="3" />
<input type="radio" id="answer3" class='radio' name="answer0" value="4" />

Then: 然后:

$('.radio').each(function(){
if($(this).prop('checked')){alert(this.id);}
});

You can see the demo of it here 你可以在这里看到它的演示

EDIT : You know, but the class dont have to be unique. 编辑:你知道,但课程不必是独一无二的。 In your loop you are not using IDs, you are using diferent class names for each element. 在你的循环中,你没有使用ID,你为每个元素使用不同的类名。 So either change the . 所以要么改变。 for # or just do it as it is shown in the Demo 对于#或只是在演示中显示它

If you want to see all radio's values , then just remove the condition 如果要查看所有无线电的值,请删除条件

$('.radio').each(function(){
alert(this.id+" "+this.value);
});

If you want to see value of selected radio only, then: 如果您只想查看所选收音机的值,则:

$('.radio').each(function(){
    if($(this).prop('checked')){alert(this.id+" "+this.value);}
});

My advice 我的建议

I think you should wrap it into the function like so 我认为你应该把它包装成这样的函数

function getSelectedRadioValue(){
    $('.radio').each(function(){
        if($(this).prop('checked')){return this.value;}
    });
}

And you can call it whenever you want 你可以随时打电话

if(getSelectedRadioValue()=='1'){
// make some magic
}else{
// more magic
}

Try this: 尝试这个:

$("input:radio[name=answer0]").click(function(){;
    alert($(this).val());
    });

IDs should be unique, add a NON-UNIQUE class for them and do the following: ID应该是唯一的,为它们添加NON-UNIQUE类并执行以下操作:

$('input.class:checked').val()

or if you don't want to use classes, use just the name you already have (just make sure IDs are unique - it's a MUST ): 或者如果你不想使用类,只使用你已经拥有的名称(只需确保ID是唯一的 - 它必须是 ):

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

EDIT: Now that you edited your original post, try this: 编辑:既然您已编辑原始帖子,请尝试以下操作:

print '<input type="radio" class="answer" id="answer'. $q . '" name="answer'. $q . '"value="1"/>' . $row['answer1'] .'<br/>';
print '<input type="radio" class="answer" id="answer'. $q . '" name="answer'. $q . '"value="2"/>' . $row['answer2'] .'<br/>';
print '<input type="radio" class="answer" id="answer'. $q . '" name="answer'. $q . '"value="3"/>' . $row['answer3'] .'<br/>';
print '<input type="radio" class="answer" id="answer'. $q . '" name="answer'. $q . '"value="4"/>' . $row['answer4'] .'<br/>';

and then to get the value just do this: 然后获取值只需执行此操作:

var value = $('input.answer:checked').val()

now value contains selected value now value包含选定的值

They all share the same ID, unique IDs is a MUST in HTML. 它们都共享相同的ID,唯一的ID 必须是HTML。 Also, it'd be more convenient to hard code individual variables for said IDs. 而且,为所述ID硬编码单个变量会更方便。

<input type="radio" id="answer0" name="answer0" value="1" />
<input type="radio" id="answer1" name="answer1" value="2" />
<input type="radio" id="answer2" name="answer2" value="3" />
<input type="radio" id="answer3" name="answer3" value="4" />

var id0 = "answer0";

var isChecked = $(id0).prop('checked');

and so on. 等等。

Use the class instead of id. 使用类而不是id。

<input type="radio" class="answer0" name="answer0" value="1" />
<input type="radio" class="answer0" name="answer0" value="2" />
<input type="radio" class="answer0" name="answer0" value="3" />
<input type="radio" class="answer0" name="answer0" value="4" />

.

$(function(){
    $('.answer0').on('change', function(){
        alert($(this).val()); 
    });
});

Here's a fiddle 这是一个小提琴

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

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