Javascript javascript选择收音机

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

javascript selected radio

javascriptradioselected

提问by Sasindu H

I want to check what is the selected radio input.

我想检查选定的无线电输入是什么。

here is my code.

这是我的代码。

<input name="u_type" type="radio" value="staff" id="u_type" checked="checked" /> Staff
<input name="u_type" type="radio" value="admin" id="u_type" /> Admin
<input id="add_user" name="add_user" type="button" onclick="addUser();"  value="Add" class="submitButton admin_add" />

function addUser()
{
//how to check what is the selected radio input
}

thanks.

谢谢。

回答by Roman Sklyarov

function addUser() {
    //how to check what is the selected radio input
    alert(getCheckedRadioValue('u_type'));
}

function getCheckedRadioValue(name) {
    var elements = document.getElementsByName(name);

    for (var i=0, len=elements.length; i<len; ++i)
        if (elements[i].checked) return elements[i].value;
}

And element's IDs must be different.

并且元素的 ID 必须不同。

回答by James Allardice

To get the value of the checked radio button, without jQuery:

要获取选中的单选按钮的值,不使用 jQuery:

var radios = document.getElementsByName("u_type");
for(var i = 0; i < radios.length; i++) {
    if(radios[i].checked) selectedValue = radios[i].value;   
}

(assuming that selectedValueis a variable declared elsewhere)

(假设这selectedValue是一个在别处声明的变量)

回答by kinakuta

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

will get you the value of the selected option which you can, of course, assign to a variable. Due to admonishment, I should also point out that this is jquery, a handy javascript library for making DOM manipulation easier and with excellent cross-browser compatibility. It can be found here.

将为您提供所选选项的值,您当然可以将其分配给变量。由于告诫,我还应该指出这是 jquery,一个方便的 javascript 库,用于简化 DOM 操作并具有出色的跨浏览器兼容性。可以在这里找到。

回答by jsonnull

Alternatively to kmb385's suggestion you could wrap your inputs in a form, and make sure all of the input names are different (you have two u_type) names.

除了 kmb385 的建议,您还可以将输入包装在一个表单中,并确保所有输入名称都不同(您有两个 u_type)名称。

Then you can access the inputs as document.formname.inputname.checked, which will return true or false.

然后您可以访问输入为document.formname.inputname.checked,这将返回 true 或 false。

回答by spekary

I know this is an old question, but the answers given seem overly complex.

我知道这是一个老问题,但给出的答案似乎过于复杂。

As pointed out earlier, you should not have two elements with the same id. That violates the html spec. Just remove the id attributes, or make them two different values. Then simply use this to get the checked value:

如前所述,您不应该有两个具有相同 id 的元素。这违反了 html 规范。只需删除 id 属性,或将它们设为两个不同的值。然后只需使用它来获取检查值:

document.querySelector("input[type='radio'][name='u_type']:checked").value

document.querySelector("input[type='radio'][name='u_type']:checked").value

回答by Kevin Bowersox

You shouldn't have two radio elements with the same id. You need to change one of the ids then check both radio buttons like:

你不应该有两个具有相同 id 的无线电元素。您需要更改其中一个 ID,然后检查两个单选按钮,例如:

if(document.getElementById("u_type").checked == true)
{
   //do something
}

I would recommend using jquery to do this instead of native js.

我建议使用 jquery 而不是原生 js 来执行此操作。

回答by Balanivash

The following is the jQuery implementation to get the value of a radio button

以下是获取单选按钮值的jQuery实现

$("#u_type").click(function() {
var value = $(this).val();
});

If you want to use javascript, then:

如果你想使用javascript,那么:

function get_radio_value()
{
for (var i=0; i < document.form.u_type.length; i++)
{
   if (document.form.u_type[i].checked)
   {
   var val = document.form.u_type[i].value;
   }
  }
}