有人看到我的If有问题吗?

jwo*_*ton -7 javascript if-statement

我在javascript中写了一个if,它检查文本框的条目是否以'at'开头.如果输入的字符串以'at' 开头,则它将运行函数showInput(),如果它不以'at'开头,则将显示错误消息.文本框的ID为"Command".我的if显示如下:

function validate(){

 var Command = 'at';

if( Command.indexOf('at')< 2 ){
    showInput();

}   
{

    else alert('Please enter valid AT command.');

}   
}
Run Code Online (Sandbox Code Playgroud)

欢呼所有人.

编辑:谢谢到目前为止!我已经尝试了所有这些但没有运气.所以我将整个文档发布在下面的代码段中.任何人都看到我的问题(留下if作为原始问题)谢谢!

<!DOCTYPE HTML>

<html>
    
<head lang="en">
    
<meta charset="UTF-8"> 

 </head>

<body>
 

    <!-- Header Image -->
    
<a href ="http://www.simplesolutions-uk.com" id="Email" target="_blank">
    
    <img src = "Header.png" alt="Simple Solutions Logo"/>
        
        </a>
    
    <!-- -->    
    
    <!-- Form Begin -->
    
 <form id="OTA">

     <!-- IMEI -->
     
    <label><font color=#829DBA face="Tahoma"><b>IMEI:</b></font></label>
     
        <input type="text" name="ID" id="IMEI" maxlength="15" >
     
     <!-- -->
     
     <!-- AT Command -->
     
    <label><font color=#829DBA face="Tahoma"><b>AT Command:</b></font></label>
     
        <input id="Command" type="text"> 
     
     <!-- -->  
     
     <!-- Response -->
     
    <label><font color=#829DBA face="Tahoma"><b>Response Needed ?</b></font></label>
     
        <input id="Check" type="checkbox" >
     
     <!-- -->
     
 </form>

    <!-- Submit Button -->
    
<input type="submit" value="Submit" style="position:relative; left:1%; top:10px;" onclick="validate(); " ><br />
    
    <!-- This is were the final string will be displayed -->
    
    <p><font color="#829DBA" face="Tahoma"</font><span id='display'></span></p> 
    
    <!-- Copy to clipboard button -->
    
    
    
    <!-- -->
    
    <!-- CSS -->
    
<style> 
    
    body { 
        
    background-color: #424242;
        
    font-size: 119%;    
        
            }
 
</style>
    
<!-- -->

<!-- Javascipt -->
    
<script language="JavaScript">    
    
// AT Validation variable
    
function validate(){

  function validate(){

 var Command = 'at';

if( Command.indexOf('at')< 2 ){
    showInput();

}   
{

    else alert('Please enter valid AT command.');

}   
}
       


// If Validate finds 'at' within the command text box then it will run this function.
    
function showInput() {

//Var test is used to change the values of the check box. If it is ticked it will display 'T' (true) or 'F' (false), this is then inputted into the string using +test+ .
    
var test=null;
    
    var obj=document.getElementById("Check").checked;
    
    obj ? test='T' : test='F';
    
//The variable below then puts all the data entered together into a >RSP string. this is what is displayed when the submit button is pressed.
        
var message_entered = ">RSP=" +test+ ";ID=" + document.getElementById("IMEI").value + ";" + document.getElementById("Command").value + "<";
    
    document.getElementById('display').innerHTML = message_entered; 
        
}
        </script>
<!-- -->
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

谢谢!

cam*_*o2k 6

你应该放在... else之间}{,删除最后一个}