根据文本输入更改表单操作
所以我有一个简单的搜索表单和提交按钮。我想要做的是验证输入以确定搜索查询中是否包含“=”,如果是,那么我希望它将输入字符串(que)发送到 vert.php。如果没有,那么我希望将其发送到 search.php。
<script type="text/javascript" language="JavaScript">
function chgact()
{
if(document.myform.que.indexOf(=) == true) {
document.myform.action = '/vert.php';
}
return true;
}
</script>
<h1>Keyword search</h1>
<form name="myform" method="post" action="/search.php" onSubmit="return chgact()">
Keywords: <br/>
<input type="text" name="que" id="que" / >
<p/>
Items to display: <br/>
<select name="i">
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
</select>
<p/>
<input type="submit" name="submit" value="Search"/>
</form>
任何帮助都将非常感激! :)
So what I have is a simple search form and submit button. What I want to do is validate the input to determine whether or not an "=" was included in the search query, if so then I want it to send the input string (que) to vert.php. If not then I would like it to send to search.php.
<script type="text/javascript" language="JavaScript">
function chgact()
{
if(document.myform.que.indexOf(=) == true) {
document.myform.action = '/vert.php';
}
return true;
}
</script>
<h1>Keyword search</h1>
<form name="myform" method="post" action="/search.php" onSubmit="return chgact()">
Keywords: <br/>
<input type="text" name="que" id="que" / >
<p/>
Items to display: <br/>
<select name="i">
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
</select>
<p/>
<input type="submit" name="submit" value="Search"/>
</form>
Any help at all would be so grealty appreciated! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将第 4 行更改为
if (document.myform.que.value.indexOf('=') != -1) {
您需要获取 que 的值,您需要用引号括起等号(您正在搜索字符'='),
indexOf
函数返回 -1(如果未找到)或第一个字符的位置出现次数,它是一个整数。它不返回布尔值。这是更新后的代码:
Change line 4 to
if (document.myform.que.value.indexOf('=') != -1) {
You need to get the value of que, you need to enquote the equals sign (you're searching for the character '='), and the
indexOf
function returns either -1 if not found or the position of the first occurrence, which is an integer. It does not return a boolean value.Here is the updated code:
我建议您使用 PHP 执行此操作。如果您的网站访问者关闭了 JavaScript,他们将无法从您的逻辑中受益。我建议您查看 strpbrk() 函数来检测“=”字符。
I would suggest you perform this action with PHP. If your site visitor has JavaScript turned off, they won't get the benefit of your logic. I suggest you look at strpbrk() function to detect the "=" char.