如何使用 Javascript 从 URL 参数自动填充文本字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12269408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:46:00  来源:igfitidea点击:

How do I auto-populate a text field from URL Parameter using Javascript

javascript

提问by user1647051

This page (http://forums.iis.net/t/1153336.aspx) explains how to do exactly what I am wanting to do, except due to the limitations of our system I only get to enter javascript to pull this information into the textbox. Can someone explain how I can use a parameter in a URL to auto fill a textbox?

这个页面 ( http://forums.iis.net/t/1153336.aspx) 解释了如何做我想做的事情,除了由于我们系统的限制,我只能输入 javascript 来提取这些信息文本框。有人可以解释我如何使用 URL 中的参数来自动填充文本框吗?

回答by Juan Mendes

You can use location.searchto access the url query and inject it into your text value

您可以使用location.search访问 url 查询并将其注入到您的文本值中

If your url is /page.htm?x=y, you can use the following code to set the value of a text field. This posttells you how to grab values from the query string

如果您的 url 是/page.htm?x=y,则可以使用以下代码来设置文本字段的值。这篇文章告诉你如何从查询字符串中获取值

// See the link above for getUrlParams
var params = getUrlParams();
document.getElementById('myTextFieldId').value = params.x;

Notice that the example you've linked to suffers from a http://en.wikipedia.org/wiki/Cross-site_scriptingvulnerability. To avoid that, you must escape the HTML before you output it on the screen

请注意,您链接到的示例存在http://en.wikipedia.org/wiki/Cross-site_scripting漏洞。为避免这种情况,您必须在将 HTML 输出到屏幕之前对其进行转义

回答by PJUK

You should be able to do something similar using windows.location in js

你应该能够在 js 中使用 windows.location 做类似的事情

http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/

http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/

回答by woz

You can easily use the answer of this questionto get parameters from the URL using JavaScript. Then set the value of your textboxes like this:

您可以轻松地使用此问题的答案从使用 JavaScript 的 URL 获取参数。然后像这样设置文本框的值:

this.form.elements["element_name"].value = 'Some Value';

回答by ItsPugle

If you want a quick solution, you might be interested in using this code snippet (inspiration taken from here, but modified by myself):

如果你想要一个快速的解决方案,你可能有兴趣使用这个代码片段(灵感来自这里,但由我自己修改):

function getURLParameter(e) {
  return decodeURI((new RegExp(e + "=(.+?)(&|$)").exec(location.search) || [, ""])[1]);
}

if (getURLParameter("inputKey") === "") {
  console.log("No URL param value found.");
} else {
  document.getElementById("inputBox").value = getURLParameter("inputkey");
}

Let's say that your url is example.com?inputKey=hello world. If so, using the above code will fill in the input with the ID of "inputBox" with the phrase "Hello world". If the URL was just example.com, it wouldn't prefill the box, and instead just act as though there was nothing (show the placeholder etc).

假设您的网址是example.com?inputKey=hello world. 如果是这样,使用上面的代码将使用短语“Hello world”填充ID为“inputBox”的输入。如果 URL 只是example.com,则它不会预填充框,而只是表现得好像什么都没有(显示占位符等)。