2015-04-06 57 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/>