简体   繁体   English

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

[英]my javascript function is not called on button click

I have this code: 我有以下代码:

<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>

but when I click the "go" button nothing happens.what could be the problem? 但是当我单击“执行”按钮时,什么也没发生。可能是什么问题? I have tried changing the type of input but it has never ran, I had a similar program that did run but I just cnt put my finger on the problem here. 我尝试过更改输入的类型,但是它从未运行过,我有一个确实运行过的类似程序,但是我只是想把问题放在这里。 please help 请帮忙

this code is working .. 该代码正在工作..

 <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>

getElementbyId is missing the capital B - should be getElementById getElementbyId缺少大写getElementById B应该是getElementById

Also the things that I mentioned in the comments need to be fixed. 另外,我在评论中提到的内容也需要修复。

You should do this: 你应该做这个:

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

Now use inputtext instead of input for comparison. 现在使用inputtext而不是input进行比较。

Or just: 要不就:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM