2012-01-19 223 views
0

對HTML很新穎,我想知道爲什麼這段代碼不起作用?Html單選按鈕

<head> 
    <title></title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
</head> 
<body> 
    <div style="font-family: Consolas;"> 
     <h1>This is a survey regarding *</h1> 
     <form name="firstQuestion" method="get" action=""> 
      <input type="radio" name="q1" value="1" id="num1">I think its 1<br> 
      <input type="radio" name="q1" value="2" id="num2">I think its 2<br> 
      <input type="radio" name="q1" value="3" id="num3">I think its 3<br> 
      <input type="radio" name="q1" value="4" id="num4">I think its 4<br> 
      <input style="position: absolute; bottom: 10px; right: 10px" type="button" value="proceed" onclick="alert('Checked value is: '+getCheckedValue(document.forms['firstQuestion'].elements['q1']))"> 
     </form> 
    </div> 
</body> 

問題是,當我按下繼續按鈕時,什麼也沒有發生,而它應該會返回所選的單選按鈕,如您所見。

現在我還在,有沒有人知道如何添加例如選項3和4之間的額外文本字段如果選擇可能性3?默認爲 ,如果未選擇3,則文本框不應該可見。

+1

getCheckedValue做什麼? – craig1231

回答

2

按鈕處理程序正在調用一個名爲getCheckedValue的javascript函數,但您尚未定義此函數。

=== UPDATE ===

<head> 
    <title></title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <script type="text/javascript"> 
     function getCheckedValue(oElements) { 
      // TODO: put in the function body here 
     } 
    <script> 
</head> 
+0

aaah當然偉大。但我應該在哪裏放這個方法。在窗體或div裏還是在我想要的任何地方? – PNS

+0

javascript函數的一個好地方是'head':' ... ' – scessor

+0

很好。我把它寫在頭部然後:) thx很多 – PNS

0

從我從你的問題收集的是,你正試圖學習HTML,但不知道你事實上是在試圖使用JavaScript來發現價值。

如果您正在使用某種教程,您需要查看getCheckedValue定義的位置並使用它。

如果你不使用教程,我會建議學習如何使用JavaScript。

我覺得W3Schools是一個很好的來源。