我正在构建一个插件,根据用户的选择更改网站的背景颜色,以供实践。我是用收音机按钮做的。我希望用户只有一个选择,就像正常的单选按钮行为一样,但在管理页面中,他可以选择几种颜色,就像复选框行为一样,甚至无法撤消所做的选择。
代码:
<div class = "wrap">
<h3>Please choose a color</h3>
<form style = "line-height:400%" method = "POST" action = "">
Black <input type="radio" name = "black" value = "Black" />
White <input type = "radio" name = "white" value = "White" />
Red <input type = "radio" name = "eed" value = "Red" />
Green <input type = "radio" name = "green" value = "Green" />
Yellow <input type = "radio" name = "yellow" value = "Yellow" /> <br/>
Orange <input type = "radio" name = "orange " value = "Orange " />
Blue <input type = "radio" name = "blue" value = "Blue" />
Pink <input type = "radio" name = "pink" value = "Pink" />
Purple <input type = "radio" name = "purple" value = "Purple" />
Brown <input type = "radio" name = "brown" value = "Brown" /><br/>
<p>Hax color<input type = "text" name = "hax" size = "5" /></p>
</form>
</div>
我该怎么修?
您面临的问题是没有将所有单选按钮链接到一个组中。这样,单选按钮"知道"当你选择其中一个时,所有其他按钮都将取消选择。您只需要为它们提供相同的name
属性。这个属性实际上是他们所在组的名称。你可以在这里看到一个例子:
http://www.echoecho.com/htmlforms10.htm
<html>
<head>
<title>My Page</title>
</head>
<body>
<form name="myform" action="action" method="POST">
<div align="center"><br>
<input type="radio" name="group1" value="Milk"> Milk<br>
<input type="radio" name="group1" value="Butter" checked> Butter<br>
<input type="radio" name="group1" value="Cheese"> Cheese
<hr>
<input type="radio" name="group2" value="Water"> Water<br>
<input type="radio" name="group2" value="Beer"> Beer<br>
<input type="radio" name="group2" value="Wine" checked> Wine<br>
</div>
</form>
</body>
</html>
名称告诉字段属于哪个单选按钮组。当您选择一个按钮时,同一组中的所有其他按钮都将被取消选择。所以对所有的单选按钮使用相同的名称。
就像
Black <input type="radio" name = "color" value = "Black" /> // here I use color as the name for all buttons
White <input type = "radio" name = "color" value = "White" />
Red <input type = "radio" name = "color" value = "Red" />
Green <input type = "radio" name = "color" value = "Green" />
Yellow <input type = "radio" name = "color" value = "Yellow" /> <br/>
Orange <input type = "radio" name = "color" value = "Orange " />
Blue <input type = "radio" name = "color" value = "Blue" />
Pink <input type = "radio" name = "color" value = "Pink" />
Purple <input type = "radio" name = "color" value = "Purple" />
Brown <input type = "radio" name = "color" value = "Brown" /><br/>