【问题标题】:How to remove trailing question mark from a GET form with no fields?如何从没有字段的 GET 表单中删除尾随问号?
【发布时间】:2012-11-21 12:49:48
【问题描述】:

例子:

<form>
    <input type='submit'>
</form>

提交结果时:

http://example.com/?

制作方法:

http://example.com/

?

[这是一个非常简单的问题示例,实际表单有很多字段,但有时会禁用一些字段。当所有都被禁用时,尾随 ?出现]

【问题讨论】:

  • 那个悬空的问号和附录一样有用......有时也很麻烦......喜欢这些变通方法! :-)
  • 我同意。这很烦人。我在 Google Chrome 中看到,但在 Internet Explorer Edge 中没有看到。原始 Chromium 错误:bugs.chromium.org/p/chromium/issues/detail?id=108690

标签: html forms get


【解决方案1】:

在我的情况下,我使用的是 window.location,不确定它是不是最好的选择,但它是我唯一可以让它工作的:

$('#myform').submit(function()
{
    ... if all parameters are empty

    window.location = this.action;
    return false;
});

我真正的用途是将 GET 参数转换为真正的 url 路径,所以这里是完整的代码:

$('#myform').submit(function()
{
    var form = $(this),
        paths = [];

    // get paths
    form.find('select').each(function()
    {
        var self = $(this),
            value = self.val();

        if (value)
            paths[paths.length] = value;

        // always disable to prevent edge cases
        self.prop('disabled', true);
    });     

    if (paths.length)
        this.action += paths.join('/')+'/';

    window.location = this.action;
    return false;
});

【讨论】:

    【解决方案2】:

    如果不使用 Javascript,我不确定是否存在。缓解该问题的一种方法可能是创建一个隐藏的输入,该输入仅包含一些垃圾值,您可以在另一边忽略这些值,如下所示:

    <input type="hidden" name="foo" value="bar" />
    

    这样你永远不会有一个空的 GET 请求。

    【讨论】:

      【解决方案3】:

      这是一个旧帖子,但是嘿.. 来吧

      如果您使用的是 PHP 之类的东西,您可以将表单提交到“代理”页面,该页面将标题重定向到特定位置 + 查询。

      例如:

      HTML:

      <form action="proxy.php" method="get"> 
         <input type="text" name="txtquery" />
         <input type="button" id="btnSubmit" />
      </form>
      

      PHP (proxy.php)

      <?php
      
         if(isset($_GET['txtquery']))
            $query = $_GET['txtquery'];
      
             header("Location /yourpage/{$query}");
      
      
      ?>
      

      我假设这是你正在尝试做的事情

      【讨论】:

        【解决方案4】:

        我知道这是一个非常古老的问题,但我今天遇到了同样的问题。我会从不同的角度来处理这个问题,我的想法是,在这个时代,您可能应该在表单中使用 POST 而不是 GET,因为在查询字符串中传递值对于安全性和 GDPR 来说并不是很好。我们已经解决了很多问题,其中各种跟踪脚本一直在获取查询字符串(参数中包含 PII),破坏了它们拥有的任何服务条款。

        通过发布,您将始终获得“干净的 url”,并且您无需对表单提交脚本进行任何修改。但是,如果需要 GET,您可能需要更改接收表单输入的任何内容。

        【讨论】:

          【解决方案5】:

          我正在寻找类似的答案。我最终做的是创建一个按钮,点击后会重定向到某个页面。

          例子:

          <button type="button" value="Play as guest!" title="Play as guest!" onclick="location.href='/play'">Play as guest!</button>
          

          这不是您问题的“答案”,但可能是一个很好的解决方法。我希望这会有所帮助。

          【讨论】:

            【解决方案6】:

            另一种选择是在提交之前使用 javascript 检查 FormData。

            var myNeatForm = document.getElementById("id_of_form");
            var formData = new FormData(myNeatForm); // Very nice browser support: https://developer.mozilla.org/en-US/docs/Web/API/FormData
            console.log(Array.from(formData.entries())); // Should show you an array of the data the form would be submitting.
            
            
            // Put the following inside an event listener for your form's submit button.
            if (Array.from(formData.entries()).length > 0) {
                dealTypesForm.submit(); // We've got parameters - submit them!
            } else {
                window.location = myNeatForm.action; // No parameters here - just go to the page normally.
            }
            

            【讨论】:

              猜你喜欢
              • 2012-10-24
              • 2019-01-10
              • 1970-01-01
              • 1970-01-01
              • 2012-12-20
              • 2015-03-08
              • 1970-01-01
              • 2012-12-19
              • 2013-07-29
              相关资源
              最近更新 更多