【问题标题】:How to redirect to mypage.com#?test=1 using html forms + javascript?如何使用 html 表单 + javascript 重定向到 mypage.com#?test=1?
【发布时间】:2016-05-12 10:35:49
【问题描述】:

我有动态处理一些数据的页面。我想使用值为test 字段的表单从一些静态页面重定向到该页面 - 我需要使用# 发送参数。我该怎么做?

<form class="search-form" method="get" action="#?">
        <input type="text" name="test" />
</form>

这个解决方案不起作用。

例如,我想在我的表单中写“1”,然后我应该被重定向到mypage.com#?test=1

是否有任何函数可以解析表单以生成 url,以便我可以重定向?或者只是从提交按钮链接中获取价值而不实际重定向?

【问题讨论】:

  • 你必须使用 javascript

标签: javascript html forms get


【解决方案1】:

试试这个:

<!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <form class="search-form" method="get" action="">
            <input type="text" id="testbox" name="test" />
            <input type="button" id="button" onclick="hello()" value="send">
        </form>
        <script type="text/javascript">
            function hello(){
                var current_url = location.protocol + '//' + location.host + location.pathname + "#";
                location.replace(current_url + "?test=" + document.getElementById('testbox').value);
            }
        </script>
    </body>
    </html>

希望这就是你的意思。

【讨论】:

  • 没有错,但不是通用的 :) 我需要更通用的解决方案。
【解决方案2】:
$(function() {
    $('button#testclick').click(function() {
        var serialized = $('form#searchByTitle').serialize();
        var url = "/search";
        location.replace(url + "#?" + serialized);
        return false;
    });
});

这就是我的解决方案。它适用于 AngularJS 路由:)

【讨论】:

    猜你喜欢
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 2011-08-14
    • 2019-02-21
    相关资源
    最近更新 更多