单击按钮时不调用我的javascript函数

时间:2013-08-15 19:10:21

标签: javascript events

我有这段代码:

<script language="javascript">
var noun=["cat","mat","rat"];
var verb=["like","is","see","sees"];
var object=["me","a+noun","the+noun"];
var subject=["I","a+noun","the+noun"];
var sentence=(subject+verb+object);

function checkCorrectness(){
    var input=document.getElementbyId("userInput");
    if(input==sentence)
        {
        alert("congratulations your sentence is correct");
        }
    else if(input=="null"||"")
        {
        alert("your entered a blank text, please enter sentence");
        }
    else{
        alert("Sorry, Your sentence is incorrect, your sentence should be in the form of a subject, verb and object. please try again");
        }
};
</script>
</head>
<body>
<h1><font size="3" color="black" face="comic sans ms">Welcome to Micro English</font></h1>
<h2><font size="2" color="blue" face="comic sans ms">Please Enter a sentence in micro English in the box below</font></h2>

<form onsubmit="checkCorrectness();">
<input type="text" name="input" id="userInput"/>
<input type="submit"value="go"/>
</form>

但是当我点击“go”按钮时没有任何反应。可能是什么问题?我已经尝试过更改输入的类型,但它从未运行过,我有一个类似的程序确实运行但我只是想知道这里的问题。请帮忙

3 个答案:

答案 0 :(得分:1)

此代码正在运行..

 <script language="javascript">
var noun=["cat","mat","rat"];
var verb=["like","is","see","sees"];
var object=["me","a+noun","the+noun"];
var subject=["I","a+noun","the+noun"];
var sentence=(subject+verb+object);

function checkCorrectness(){
    var input=document.getElementById("userInput").value;
    if(input==sentence.length)
        {
        alert("congratulations your sentence is correct");
        }
    else if(input==null || input =="")
        {
        alert("your entered a blank text, please enter sentence");
        }
    else{
        alert("Sorry, Your sentence is incorrect, your sentence should be in the form of a subject, verb and object. please try again");
        }
};
</script>
</head>
<body>
<h1><font size="3" color="black" face="comic sans ms">Welcome to Micro English</font></h1>
<h2><font size="2" color="blue" face="comic sans ms">Please Enter a sentence in micro English in the box below</font></h2>

<form onsubmit="checkCorrectness();">
<input type="text" name="input" id="userInput"/>
<input type="submit"value`enter code here`="go"/>
</form>

答案 1 :(得分:0)

getElementbyId缺少首都B - 应该是getElementById

我在评论中提到的内容也需要修复。

答案 2 :(得分:0)

你应该这样做:

var input=document.getElementbyId("userInput");
 var inputtext=input.value;

现在使用inputtext而不是输入进行比较。

或者只是:

var input=document.getElementbyId("userInput").value;