2015-10-15 214 views
-2

程序唯一的問題是沒有選擇顏色,顏色總是黑色,我將單選按鈕更改爲單個文本輸入元素並且它工作,所以錯誤必須是相關的到單選按鈕。單選按鈕顏色選擇器

function hello() { 
    var name = document.getElementById("name").value; 
    var para = document.getElementById("para"); 

    var myColor = ""; 

    var color = { 
     green: document.getElementById("green"), 
     blue: document.getElementById("blue"), 
     red: document.getElementById("red") 
    } 

    switch (color) { 

     case color.green.checked: 
      myColor = "green"; 
      break; 

     case color.blue.checked: 
      myColor = "blue"; 
      break; 

     case color.red.checked: 
      myColor = "red"; 

     default: 
      myColor = "black" 

    } 

    para.style.color = myColor; 

    if (name == "") { 
     para.innerHTML = "Incorrect Input"; 
    } else { 
     para.innerHTML = "Hello " + name + "!"; 
    } 

} 

回答

0

切換使用您比較鍵值與案例。在這個代碼中,它不等於任何一種情況,所以他選擇了默認值。

試試這個:

myColor = "black" 

if(color.green.checked) 
    myColor = "green"; 
else if(color.blue.checked) 
    myColor = "blue"; 
else if(color.red.checked) 
    myColor = "red"; 

para.style.color = myColor;