对表单中的文本进行编码...有什么解决方案吗?

发布于 2025-01-03 19:33:10 字数 501 浏览 2 评论 0原文

我有一个非常简单的 HTML 表单,它将获取文本框的值并创建一个 URL。 这是我的表单:

    <form id="searchForm" method="GET" action="http://searchserver/results.aspx target="_blank">
            <input type="text" name="p" size="18" maxlength="256" value="hello world"/>
</form>

如您所见,当按 Enter 键时,它将值发送到新浏览器,其 url 为:“http://searchserver/results.aspx?p=hello+world”

有没有办法强制此处的加号 (+) 是否插入到 %20 字符中?所以它会是:“http://searchserver/results.aspx?p=hello*%20*world”

谢谢

I do have a very simple HTML form that would take the value of the textbox and create a URL.
Here is my form:

    <form id="searchForm" method="GET" action="http://searchserver/results.aspx target="_blank">
            <input type="text" name="p" size="18" maxlength="256" value="hello world"/>
</form>

As you can see, when hitting enter, it sends the value to the new browser with the url as: "http://searchserver/results.aspx?p=hello+world"

Is there a way to force the plus (+) sign here into the %20 character? So it would be: "http://searchserver/results.aspx?p=hello*%20*world"

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

嗫嚅 2025-01-10 19:33:10

这就是在没有任何框架的情况下如何做到这一点:

var form = document.getElementById('searchForm');
var el = form.elements[0];

document.location = form.action + "?" + el.name + "=" + encodeURIComponent(el.value);

享受。

This is how you'd do it without any framework:

var form = document.getElementById('searchForm');
var el = form.elements[0];

document.location = form.action + "?" + el.name + "=" + encodeURIComponent(el.value);

Enjoy.

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