Using Javascript to pass a parameter from a URL to a hidden form field - Stack Overflow

I wasn to be able to pass and ID such as '123456' from a URL:.html?ID=123456to a hidden form

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

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

Any ideas much appreciated!

The JS I am using is:

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

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

http://www.mysite./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.

Any ideas much appreciated!

The JS I am using is:

<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>
Share Improve this question edited Nov 24, 2011 at 2:25 Lawrence asked Nov 24, 2011 at 1:55 LawrenceLawrence 1492 gold badges2 silver badges8 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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

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

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

without jQuery you would do like this:

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

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

  <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" />
window.location.search

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

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

Anything else or is that enough to go on?

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744275663a4566316.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信