我在javascript中写了一个if,检查文本框的条目是否以'at'开头。如果输入的字符串以“at”开始,那么它将运行函数showInput()
,如果它不以'at'开头,则会显示错误消息。文本框的ID为“命令”。我的if显示如下:
function validate(){
var Command = 'at';
if( Command.indexOf('at')< 2 ){
showInput();
}
{
else alert('Please enter valid AT command.');
}
}
为所有人欢呼。
编辑:到目前为止,谢谢!我已经尝试了所有这些但没有运气。所以我将整个文档发布在下面的代码段中。有人看到我的问题(将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>
谢谢!
答案 0 :(得分:6)
您应该将else
放在}{
之间..并删除最后}
答案 1 :(得分: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.');
}
}
答案 2 :(得分:2)
您在else
使用了太多括号,这是错误的。
function validate(){
var Command = 'at';
if( Command.indexOf('at')< 2 ){
showInput();
} else {
alert('Please enter valid AT command.');
}
}