使用 jQuery 获取 URL 参数并将它们显示在 HTML 输入文本字段中 [英] Get URL parameters with jQuery and display them inside an HTML input text field
本文介绍了使用 jQuery 获取 URL 参数并将它们显示在 HTML 输入文本字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里和谷歌上浏览了数十篇关于此的文章,但似乎没有一篇有用.我正在尝试从 URL 获取参数并将它们显示在输入文本字段中.
I been through dozens of articles on this on here and on Google but none of them seemed to have worked. I am trying to get parameters from the URL and display them inside an input text field.
这是我正在处理的代码:
This is the code I'm working on:
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
getQueryVariable("inputline1");
getQueryVariable("inputline2");
<input type="text" id="inputline1" value="getQueryVariable('inputline1')" />
<input type="text" id="inputline2" value="getQueryVariable('inputline2')" />
我正在尝试的网址:
?inputline1=sampletext1&inputline2=sampletext2
我的 jQuery 知识非常有限,任何有关正确方向的帮助都会非常有帮助.
My jQuery knowledge is very limited and any help on the right direction on this would be very helpful.
提前致谢.
推荐答案
终于可以工作了,更新了下面的工作脚本以帮助将来的任何人:
Got it finally to work, updated a working script below to help anyone in the future:
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i = 0; i < parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos > 0 && variable == parms[i].substring(0, pos)) {
return parms[i].substring(pos + 1);;
}
}
return "";
}
getQueryVariable("inputline1");
$(function () {
$('#inputline1').val(getQueryVariable('inputline1'))
});
<input type="text" id="inputline1" />
带参数的网址:
?inputline1=Sample
这篇关于使用 jQuery 获取 URL 参数并将它们显示在 HTML 输入文本字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文