简体   繁体   English

从url变量填充输入字段-使用javascript或jquery

[英]populate input field from url variable - using either javascript or jquery

I have a url 我有一个网址

http://www.example.com/state=survey&action=display&survey=39&zone=surveys&currency=Denmark http://www.example.com/state=survey&action=display&survey=39&zone=surveys&currency=丹麦

A user will land on a new page with the above url. 用户将进入具有上述网址的新页面。 This page will include a form. 此页面将包含一个表格。 There is an input with an id 'currencyrequired' - i want this input to automatically pull in the currency variable from the url (in this example Denmark). 有一个ID为'currencyrequired'的输入-我希望此输入自动从url中提取货币变量(在此示例中为Danmark)。

<input type="text" id="currencyrequired" name="currencyrequired" size="40" maxlength="20" value="" class="input-pos-int">

The currency part of the url is likely to change as it depends upon which country they select so you could end up with - http://www.example.com/state=survey&action=display&survey=39&zone=surveys&currency=Norway 网址的货币部分可能会更改,因为这取决于他们选择的国家/地区,因此您可能会获得-http: //www.example.com/state=survey&action=display&survey=39&zone=surveys&currency=Norway

Ideally what i would like is to list all the currencies (8 in total) and the script would check what country is listed in the url and populate the currencyrequired input field with the output 理想情况下,我要列出所有货币(总共8种),脚本将检查url中列出的国家/地区,并用输出填充currencyrequired输入字段

You could use location.search to return the query string and do: 您可以使用location.search返回查询字符串并执行以下操作:

var url = location.search;
var country = url.substring(url.indexOf('currency=')+9, url.length);

$("#input").val(country);

http://jsfiddle.net/ckJ5S/ http://jsfiddle.net/ckJ5S/

Try this: 尝试这个:

$("#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, " "));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM