从url变量填充输入字段-使用javascript或jquery [英] populate input field from url variable - using either javascript or jquery

查看:50
本文介绍了从url变量填充输入字段-使用javascript或jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网址

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

用户将进入具有上述网址的新页面.此页面将包含一个表格.有一个ID为'currencyrequired'的输入-我希望此输入自动从网址(在本例中为丹麦)中提取货币变量.

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).

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

URL的货币部分可能会更改,这取决于它们选择的国家/地区,因此您可能会得到-

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

理想情况下,我想列出所有货币(总共8种),脚本将检查url中列出了哪个国家/地区,并用输出填充Currencyrequired输入字段

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

推荐答案

尝试一下:

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

这篇关于从url变量填充输入字段-使用javascript或jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆