Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在html中为无线电组列表命名无线电?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何在html中为无线电组列表命名无线电?

Javascript 如何在html中为无线电组列表命名无线电?,javascript,jquery,html,Javascript,Jquery,Html,我希望它会发出一个颜色列表。在html中,若输入是列表,那个么它们应该是相同的名称,但无线电输入的名称相同,它们将位于同一个组中,所以如何将每个无线电组分开,但所有组仍然具有相同的名称,那个么我可以使用循环在服务器端获取列表的所有值 此外,在客户端,我需要检查是否选择了每个组,因此如何使用javascript或jquery选择所有单选组,而不是所有单选按钮 <form method="post"> Car A: <input type="radio" name="color[]

我希望它会发出一个颜色列表。在html中,若输入是列表,那个么它们应该是相同的名称,但无线电输入的名称相同,它们将位于同一个组中,所以如何将每个无线电组分开,但所有组仍然具有相同的名称,那个么我可以使用循环在服务器端获取列表的所有值

此外,在客户端,我需要检查是否选择了每个组,因此如何使用javascript或jquery选择所有单选组,而不是所有单选按钮

<form method="post">
Car A: <input type="radio" name="color[]"/>red <input type="radio" name="color[]"/>yello<br/>
Car B: <input type="radio" name="color[]"/>red <input type="radio" name="color[]"/>yello<br/>
Car C: <input type="radio" name="color[]"/>red <input type="radio" name="color[]"/>yello<br/>
<input type="submit"/>
</form>
Car A: 
<input type="radio" name="color1"/>red 
<input type="radio" name="color1"/>yello<br/>

Car B: 
<input type="radio" name="color2"/>red 
<input type="radio" name="color2"/>yello<br/>

Car C: 
<input type="radio" name="color3"/>red 
<input type="radio" name="color3"/>yello<br/>

A车:红色耶罗
B车:红色耶洛
C车:红色耶洛

每个组的所有单选按钮都应有不同的名称

<form method="post">
Car A: <input type="radio" name="color[]"/>red <input type="radio" name="color[]"/>yello<br/>
Car B: <input type="radio" name="color[]"/>red <input type="radio" name="color[]"/>yello<br/>
Car C: <input type="radio" name="color[]"/>red <input type="radio" name="color[]"/>yello<br/>
<input type="submit"/>
</form>
Car A: 
<input type="radio" name="color1"/>red 
<input type="radio" name="color1"/>yello<br/>

Car B: 
<input type="radio" name="color2"/>red 
<input type="radio" name="color2"/>yello<br/>

Car C: 
<input type="radio" name="color3"/>red 
<input type="radio" name="color3"/>yello<br/>
您可以将选择器仅限于其
名称
属性以
颜色
开头的元素:

if ($('input[type="radio"][name^="color"]:checked').length == 3)

每个组的所有单选按钮都应有不同的名称

<form method="post">
Car A: <input type="radio" name="color[]"/>red <input type="radio" name="color[]"/>yello<br/>
Car B: <input type="radio" name="color[]"/>red <input type="radio" name="color[]"/>yello<br/>
Car C: <input type="radio" name="color[]"/>red <input type="radio" name="color[]"/>yello<br/>
<input type="submit"/>
</form>
Car A: 
<input type="radio" name="color1"/>red 
<input type="radio" name="color1"/>yello<br/>

Car B: 
<input type="radio" name="color2"/>red 
<input type="radio" name="color2"/>yello<br/>

Car C: 
<input type="radio" name="color3"/>red 
<input type="radio" name="color3"/>yello<br/>
您可以将选择器仅限于其
名称
属性以
颜色
开头的元素:

if ($('input[type="radio"][name^="color"]:checked').length == 3)

组中的每个成员必须具有相同的名称。每个组必须有不同的名称

如果您想迎合PHP的命名约定来在循环中显示数据,那么使用一种约定,例如:第一个组使用
group[0]
,第二个组使用
group[1]
,依此类推。这会在客户端上为您提供唯一的名称,而PHP会将它们合并到一个数组中

在客户端,您可以执行以下操作:

var i = 0, myform = /* something */;
while (myform.elements['group[' + i + ']']) {
    var group = myform.elements['group[' + i + ']'];
    /* Loop over HTML Element Collection to make sure one item is `checked`. */
}

组中的每个成员必须具有相同的名称。每个组必须有不同的名称

如果您想迎合PHP的命名约定来在循环中显示数据,那么使用一种约定,例如:第一个组使用
group[0]
,第二个组使用
group[1]
,依此类推。这会在客户端上为您提供唯一的名称,而PHP会将它们合并到一个数组中

在客户端,您可以执行以下操作:

var i = 0, myform = /* something */;
while (myform.elements['group[' + i + ']']) {
    var group = myform.elements['group[' + i + ']'];
    /* Loop over HTML Element Collection to make sure one item is `checked`. */
}

你不能每个广播组都必须使用不同的名称,否则你怎么知道何时收到表单,哪种颜色是哪辆车的颜色。你可以使用php获取输入值,然后在表单提交时与你要做的任何事情进行比较。在php中,这可能会转化为一个数组。jQuery是一个JavaScript库,不是JavaScript的替代品。“javascript或jquery”这句话毫无意义。你不能让每个广播组都使用不同的名称,否则你怎么知道何时收到表单,哪种颜色是哪辆车的颜色。你可以使用php获取输入值,然后在表单提交时与你要做的任何事情进行比较。在php中,这可能会转化为一个数组。jQuery是一个JavaScript库,不是JavaScript的替代品。短语“javascript或jquery”毫无意义。