2014-12-03 59 views
0

我正在编写一个测验页面以在浏览器中显示,当用户点击正确时,onclick会给出“正确”消息,否则“不再尝试”,我想使代码更简单。如何将一个规则集应用于多个功能

function colorPurple9() { 

    var Morado = document.getElementById('C9').value; 
      if (document.getElementById('C9').checked) { 
      alert("Yes!") 
       } 
      else { 
      alert("Sorry, You got the wrong answer.") } 

如何列出多种功能(例如:functionOne,colorTwo等)中/上一组的规则,使我没有使用的代码洙多行。在表单框中,我使用带有ID标签的收音机,在上面的代码中它是“C9”(和下面的“N2”),我想知道我是否也可以列出更多的ID,如数组或其他内容,以函数定义。

<p>The word for Two in spanish is: 
    <br /><input name="word" type="radio" value="False" onclick="numberTwo()"/>Cuatro 
    <br /><input name="word" type="radio" value="False" onclick="numberTwo()"/>Cinco 
    <br /><input name="word" id="N2" type="radio" value="True" onclick="numberTwo()"/>Dos 
    <br /><input name="word" type="radio" value="False" onclick="numberTwo()"/>Diez</p> 

而且我不知道如何强制他们的第一选择是永久性的,或者使用一个计数器,但这是可选的。请。

+0

您的意思是这个标签作为'javascript'?我不明白为什么这个标签是'java' – 2014-12-03 05:26:25

+0

这里有一系列的问题。 – Secondo 2014-12-03 05:31:13

回答

0

试试这个。

var message = (document.getElementById('C9').checked)?"Yes!":"Sorry, You got the wrong answer."; 
alert(message); 

for some info go here,

希望这将帮助你:))

0

你可以试试这个代码,希望它会为你的问题的工作

<script> 
    function numberTwo(){ 
     var temp= document.querySelector('input[name="word"]:checked').value; 
     var result = (temp == "True") ? "Yes!" : "Sorry, You got the wrong answer."; 
     alert(result); 
    } 
</script> 
相关问题