两个单选按钮共享一个“ID”? [英] Two radio buttons share one "id"?

查看:86
本文介绍了两个单选按钮共享一个“ID”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我借用/修改了这个简单的html / javascript表单来设置数据库中的一些数据。原始代码使用表单中的文本字段,但我使用单选按钮。下面的前三个步骤是原创的,我的问题出现在......之后,我是否会给单选按钮添加相同的ID ...希望这是明确的...



第1步。用户将值输入到ID为nick的表单中。

 < tr> 
< td>< label>使用者< / label>< / td>
< td>< input class =text userid =nicktype =textMAXLENGTH =25/>< / td>
< / tr>

第2步。使用id分配给变量的idnick的值

  var inputUser = $(#nick); 

步骤3.从变量中获取值以插入数据库...

  if(inputUser.attr(value)

,但是如果它是两个单选按钮而不是一个文本字段....

 < td>< tt>< t>< td>< / label>< / td> 
< td>< input type =radioname =interviewid =nick /> $>< / td>
< td>< input type =radioname =interviewid =nickvalue =fail/>失败< / td>

我是否将单选按钮赋予相同的id,以便在将值赋给变量...

pre $ $ $ $ c $ var $ input $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'

这样,无论哪个按钮被选中都会被分配到IDnick中?

解决方案

不,Id属性应该始终是唯一的。如果您使用jQuery(厕所ks像你一样),你可以用 $('input [name = interview]'); 来选择它。


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...

Step 1. User enters value into form with 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

var inputUser = $("#nick");  

Step 3. getting the value from the variable for insertion into database...

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...

var inputUser = $("#nick"); 

so that whichever button is checked will be assigned found in the id "nick"?

解决方案

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”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆