Javascript 两个单选按钮共享一个“id”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8305953/
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
Two radio buttons share one "id"?
提问by Leahcim
I'm borrowing/adapting this simple html/javascript form set up to put some data in the database. The original code uses text fields in the form, but I'm using radio buttons. The first three steps below are the original, and my question comes after...namely, do I give the radio buttons the same id...Hope this is clear...
我正在借用/改编这个简单的 html/javascript 表单来将一些数据放入数据库中。原始代码使用表单中的文本字段,但我使用的是单选按钮。下面的前三个步骤是原始的,我的问题是在……即,我是否给单选按钮赋予相同的 ID……希望这很清楚……
Step 1. User enters value into form with id "nick"
步骤 1. 用户将值输入到表单中,id 为“nick”
<tr>
<td><label>User</label></td>
<td><input class="text user" id="nick" type="text" MAXLENGTH="25" /></td>
</tr>
Step 2. Value associated with id "nick" assigned to variable using id
步骤 2. 使用 id 将与 id "nick" 关联的值分配给变量
var inputUser = $("#nick");
Step 3. getting the value from the variable for insertion into database...
步骤 3. 从变量中获取值以插入数据库...
if(inputUser.attr("value")
but if it's two "radio buttons" rather than one "text" field....
但如果它是两个“单选按钮”而不是一个“文本”字段......
<td><label>Interview</label></td>
<td><input type="radio" name="interview" id="nick" value="pass" />Pass</td>
<td><input type="radio" name="interview" id="nick" value="fail" /> Fail</td>
Do I give the radio buttons the same "id" so that it's still like this when I assign the value to the variable...
我是否为单选按钮提供了相同的“id”,以便当我将值分配给变量时它仍然是这样的......
var inputUser = $("#nick");
so that whichever button is checked will be assigned found in the id "nick"?
以便在 id“nick”中找到被选中的任何按钮?
回答by Matthew
No, an Id attribute should always be unique. If you're using jQuery (looks like you are), you can select it with $('input[name=interview]');
.
不,Id 属性应该始终是唯一的。如果您使用的是 jQuery(看起来像您),则可以使用$('input[name=interview]');
.
回答by nnnnnn
Since you are using jQuery you can easily get the value of the selected radio button by using the :checkedselector:
由于您使用的是 jQuery,您可以使用:checked选择器轻松获取所选单选按钮的值:
$("input[name=interview]:checked").val()
You should definitely not give more than one element the same ID (that's invalid HTML and will lead to confusing bugs in your JavaScript), but even if that worked it wouldn't help in this case since radio buttons as a group don't have a selected value: you need to determine which one is currently checked and then get its value as shown above. (This is not a problem when getting the value on the webserver when the form is actually submitted, because only the value from the checked radio gets submitted.)
您绝对不应该给多个元素提供相同的 ID(这是无效的 HTML,会导致您的 JavaScript 中出现令人困惑的错误),但即使这样做有效,在这种情况下也无济于事,因为作为一组的单选按钮没有a selected value:您需要确定当前选中的是哪个,然后获取其值,如上图所示。(在实际提交表单时获取网络服务器上的值时,这不是问题,因为只有来自选中单选框的值才会被提交。)
Note also that in your original code where you said inputUser.attr("value")
, you could've said inputUser.val()
.
另请注意,在您所说的原始代码中inputUser.attr("value")
,您可以说inputUser.val()
.
回答by Selvakumar Ponnusamy
ID Attribute is unique across the page. You should have different Ids for each radio button. Use below code to get the input value.
ID 属性在整个页面中是唯一的。每个单选按钮应该有不同的 ID。使用以下代码获取输入值。
var inputUser=$('input:radio[name=interview]:checked').val();
回答by Md. Shafiqur Rahman
If want to use pure javascript you have to use..
如果想使用纯 javascript,你必须使用..
document.querySelector('input[name=radio_btn_name]');
but it need updated browser. Old browsers may result incorrect.
但它需要更新浏览器。旧浏览器可能会导致错误。
To overcome this issue, javascript library is to use (library will handle the issue). For this use the following..
为了克服这个问题,需要使用 javascript 库(库会处理这个问题)。为此使用以下..
$('input[name=radio_btn_name]:checked').val();
回答by Rohan Patil
Do not repeat id's.It is best practice to use only one id per page as it must be unique.You can group the radio buttons using name attribute.
不要重复 id。最好每页只使用一个 id,因为它必须是唯一的。您可以使用 name 属性对单选按钮进行分组。
use $('input[name=interview]')
to get the value.
用于$('input[name=interview]')
获取值。
回答by diEcho
you must assign same name but different id to input type=radio
because this is basic property of raido button that you need to select only one value from givel aal radio buttons.
您必须为其分配相同的名称但不同的 ID,input type=radio
因为这是 raido 按钮的基本属性,您只需从 gail aal 单选按钮中选择一个值。
but that id should not be assigned to other form elements
但不应将该 id 分配给其他表单元素
回答by Trott
The id
assigned to an element must be unique within the document. So, no, you should not use the same id
.
该id
分配给一个元素必须是文档中是唯一的。所以,不,你不应该使用相同的id
.
回答by Wazy
Insteadof id="nick"
相反的id="nick"
You can use name="nick"
您可以使用 name="nick"
Or
或者
class="nick"