2016-01-06 62 views
-7

我編寫了一個if在javascript中檢查文本框的條目是否以'at'開頭。如果輸入的字符串與'at'開始,則它將運行功能showInput(),如果它不以'at'開始,則會顯示錯誤消息。文本框的ID是「Command」。我的如果顯示如下:任何人都看到我的問題?

function validate(){ 

var Command = 'at'; 

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

} 
{ 

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

} 
} 

歡呼聲。

編輯:到目前爲止感謝!我試過所有這些,但沒有運氣。所以我已經將整個文檔發佈在下面的代碼片段中。任何人看到我的問題(離開如果原來的一個) 謝謝!

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

謝謝!

+3

是的,我看到一個問題 – lascort

+0

但它不是在'if'。它是否算數? –

+4

它是'if(){} else {}'not'if(){} {else stuff}' – lascort

回答

6

你應該把else}{之間..和刪除最後一個}

2

您在使用else太多的括號,這是錯誤的。

function validate(){ 

    var Command = 'at'; 

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

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

更多類似這樣的:

var Command = "at";  // presumably you actually get this from something else 
validate(Command); 

function validate(command) { 
    if (command.indexOf('at') == 0) { 
     showInput(); 
    } else { 
     alert('Please enter valid AT command.'); 
    } 
} 
+0

乾杯隊友,仍然沒有按預期運行..如果你不介意我上面編輯了我的問題,謝謝 – jwollerton

相關問題