根据文本输入更改表单操作

发布于 2024-12-27 03:24:02 字数 941 浏览 3 评论 0原文

所以我有一个简单的搜索表单和提交按钮。我想要做的是验证输入以确定搜索查询中是否包含“=”,如果是,那么我希望它将输入字符串(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩起发的微风 2025-01-03 03:24:03

将第 4 行更改为 if (document.myform.que.value.indexOf('=') != -1) {

您需要获取 que 的,您需要用引号括起等号(您正在搜索字符'='),indexOf 函数返回 -1(如果未找到)或第一个字符的位置出现次数,它是一个整数。它不返回布尔值。

这是更新后的代码:

<script type="text/javascript" language="JavaScript">
function chgact()
   {
if(document.myform.que.value.indexOf('=') != -1) {
   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>

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:

<script type="text/javascript" language="JavaScript">
function chgact()
   {
if(document.myform.que.value.indexOf('=') != -1) {
   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>
凉薄对峙 2025-01-03 03:24:03

我建议您使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文