2012-01-19 224 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是一个很好的来源。