使用 Javascript 将参数从 URL 传递到隐藏的表单字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8251393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:47:36  来源:igfitidea点击:

Using Javascript to pass a parameter from a URL to a hidden form field

javascripthidden-field

提问by Lawrence

I wasn to be able to pass and ID such as '123456' from a URL:

我无法通过 URL 传递 ID,例如“123456”:

http://www.mysite.com/page.html?ID=123456

http://www.mysite.com/page.html?ID=123456

to a hidden form field.

到一个隐藏的表单域。

I found a Javascript that lets me write the ID to a page but I don't know how to add the value to the value="" bit of a hidden input field.

我找到了一个 Javascript,它可以让我将 ID 写入页面,但我不知道如何将值添加到隐藏输入字段的 value="" 位。

Any ideas much appreciated!

任何想法都非常感谢!

The JS I am using is:

我正在使用的 JS 是:

<script> 
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]; 
    }
  } 
  alert('Query Variable ' + variable + ' not found'); 
}   
</script>

回答by Cyclonecode

If you already have the value you could do like this with jQuery:

如果您已经拥有可以使用 jQuery 执行此操作的值:

 $('#your-input-field').val(the_value);

 <input type="hidden" id="your-input-field" />

without jQuery you would do like this:

如果没有 jQuery,你会这样做:

 e = document.getElementById('your-input-field');
 e.value = the_value;

Here is an example using your code, I recommend that you look into jQuery - it's much easier to use than plain javascript and your code will work on all browsers:

这是使用您的代码的示例,我建议您查看 jQuery - 它比普通 javascript 更容易使用,并且您的代码将适用于所有浏览器:

  <script type="text/javascript">
  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]; 
     }
   } 
 }
 function onLoad() {
    var value = getQueryVariable("ID");
    var e = document.getElementById('your-field');
    e.value = value;
 }
 </script>
 <body onload="onLoad()">
     <!-- your form and hidden field goes here -->
     <input type="hidden" name="your-field" id="your-field" />

回答by Matthew

window.location.search

...will give you the query string part of your URL

...会给你你的 URL 的查询字符串部分

So in this case it will return "?ID=123456".

所以在这种情况下,它会返回“?ID=123456”。

Anything else or is that enough to go on?

还有什么或者这足以继续下去吗?

回答by Ricardo Marimon

This link that uses a jQUery might help you url-parameters. Once you have the value you can do:

这个使用 jQUEry 的链接可能会帮助你url-parameters。获得价值后,您可以执行以下操作:

$('#input-id').val($.getUrlVar('url-parameter'));