2015-04-06 55 views
-2

我正在构建一个插件,用于根据用户的选择更改网站的背景颜色,供练习使用。我正在用单选按钮来做。 我希望用户只有一个选择,就像普通的单选按钮行为一样,但在管理页面中,他可以选择多种颜色,如复选框行为,甚至无法撤消选择的选择。WP管理页面上的单选按钮

代码:

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

我怎样才能解决这个问题?

+3

用于所有单选按钮同名 – Yatendra 2015-04-06 09:16:09

+1

请开始搜索您所选择的HTML参考。您可以先阅读书籍或了解高质量的在线资源。在*之后询问你的方式*开始时没有先考虑哪些标签用于什么以及如何工作并不是一个好主意。特别是因为这些问题会让你提出的问题在很大程度上不在Stackoverflow上。所以先制定一个计划,然后编写代码。 – hakre 2015-04-06 09:26:49

回答

0

您面临的问题是您没有将所有单选按钮链接到一个组中。通过这种方式,单选按钮“知道”,当你选择其中一个时,其他所有其他人将取消选择。你只需要给他们所有相同的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> 
0

名称告诉领域属于哪个单选按钮组。当您选择一个按钮时,同一组中的所有其他按钮都将被取消选中。所以对所有单选按钮使用相同的名称。

就像,

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