从URL变量填充输入字段 - 使用JavaScript或jQuery的
我有一个网址从URL变量填充输入字段 - 使用JavaScript或jQuery的
http://www.example.com/state=survey&action=display&survey=39&zone=surveys¤cy=Denmark
的用户将在新页面上登陆上述网址。这个页面将包含一个表单。有一个id为'currencyrequired'的输入 - 我希望这个输入能自动从url中取出货币变量(在这个例子中是丹麦)。
<input type="text" id="currencyrequired" name="currencyrequired" size="40" maxlength="20" value="" class="input-pos-int">
URL的货币部分很可能会改变,因为它取决于他们选择的国家,所以你可能最终与 - http://www.example.com/state=survey&action=display&survey=39&zone=surveys¤cy=Norway
理想我想什么是列出所有货币(8在总)和脚本将检查哪个国家在URL中列出,并与输出填充currencyrequired输入字段
你可以使用location.search
返回查询字符串,并做到:
var url = location.search;
var country = url.substring(url.indexOf('currency=')+9, url.length);
$("#input").val(country);
这将只适用于如果货币=丹麦是网址中的最后信息。 – TryingToImprove 2013-04-24 09:30:12
这适用于jsfiddle,但是当我将代码存入我的页面时,它不会执行任何操作。 – 2013-04-24 10:13:35
@ R4N_S_S - 你的页面上是否定义了jquery?尝试添加提醒。警报(国家);' - 警报是否工作? – 2013-04-24 10:16:36
试试这个:
$("#currencyrequired").val(getParameterByName('currency'));
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
我测试过它,它适用于所有浏览器。您的输入元素也必须以/>结尾 – Elio 2013-04-24 11:30:11
我需要得到这个固定_Yesterday_? – 2013-04-24 09:20:07
可能重复的[如何从URL参数获取值?](http://stackoverflow.com/questions/979975/how-to-get-the-value-from-url-parameter) – Quentin 2013-04-24 09:26:12
@TheVal - 此项目这个星期到期,我需要尽快把这个功能推出来,因此紧迫感会很强。我是一个相对的新手与JavaScript/jQuery的,所以任何帮助表示赞赏 – 2013-04-24 10:08:49