【发布时间】:2011-03-19 01:49:28
【问题描述】:
我有一个带有文本输入和提交按钮的表单。在文本输入中,您可以编写简单的命令,例如 /look(创建一个简单的游戏,以尝试并提高我的技能)。 在 .js 文件中,我有一个包含一些命令/字符串的数组。
我的问题是:如何将表单中的字符串与数组中的字符串进行匹配。 到目前为止,我已经计算出我需要一个 for 字符串和一个 if/else 字符串,但我不知道该怎么做。
html 文件:
<div id="commandField">
<form method="POST" action="action" onSubmit="return commands(str);">
<p class="center">Command: <input type="text" name="command" class="command" /><input type="submit" value="Execute" /></p>
</form>
</div>
javascript 文件:
function commands(str)
{
var charCommand=new Array(); // regular array (add an optional integer
charCommand[0]="/look"; // argument to control array's size)
charCommand[1]="/use";
charCommand[2]="/continue";
charCommand[3]="/pickup";
for(i=0 ; i < charCommand.length ; i++)
{
if(str.match(charCommand[x]))
{
document.getElementById("commandField").innerHTML=charCommand[x];
}
}
}
【问题讨论】:
-
嗨,你的问题是如何将表单元素的值传递给函数?
-
首先弹出的是你的
for循环。在您的参数中,您有变量i但str.match(charCommand[x])正在使用x?
标签: javascript arrays string forms match