我有以下 URL:http://localhost:8888/datatest?username=JD042719
我想拉入“JD042719”
。
此问题不需要“获取”请求。
然后,用户名值应设置为另一个文本框的值。也许是这样的:
HTML
<label for="Assoc">Associate ID</label>
<input type="text" name="AssocID" id="AssocID" required="required">
JavaScript
$(function associd(){
document.getElementById("AssocID").value=username;
});
最佳答案
新答案(2021)
下面的答案已经过时了。除非您需要支持 Internet Explorer,否则您应该使用 URLSearchParams如下:
var username = (new URLSearchParams(window.location.search)).get("username");
旧答案(2015)
使用 this answer提供的优秀功能,就像这样:
使用
var username = getParameterByName("username");
函数
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
变量username
现在应该保存值JD042719
(或URL中的任何值)。
关于javascript - 从 URL 中提取变量并将其设置为文本框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31522621/