最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to set the value of a form element using Javascript? - Stack Overflow

programmeradmin0浏览0评论

I have a simple form like this one:

<form ENCTYPE="multipart/form-data" action="upload.php" method="POST">
<p>UPLOAD FILE: <input type="file" name="file1"> <br /></p>
<input type="submit" value="Upload">
<input type="hidden" name="email" id="email" value="">
</form>

I have to set the value of the hidden field to an email like "[email protected]". I can obtain this value from adding a querystring parameter by external iframe like:

<iframe name="[email protected]" src="[email protected]">

Ok. But how can I set value="" to value="[email protected]"? I know have to use javascript, but I dunno how to deal with the var.

I have a simple form like this one:

<form ENCTYPE="multipart/form-data" action="upload.php" method="POST">
<p>UPLOAD FILE: <input type="file" name="file1"> <br /></p>
<input type="submit" value="Upload">
<input type="hidden" name="email" id="email" value="">
</form>

I have to set the value of the hidden field to an email like "[email protected]". I can obtain this value from adding a querystring parameter by external iframe like:

<iframe name="[email protected]" src="[email protected]">

Ok. But how can I set value="" to value="[email protected]"? I know have to use javascript, but I dunno how to deal with the var.

Share Improve this question edited Jul 9, 2012 at 10:50 Gareth 138k36 gold badges151 silver badges158 bronze badges asked Jul 9, 2012 at 10:44 user840718user840718 1,6117 gold badges36 silver badges61 bronze badges 2
  • You could also fill it in on the server side if you want - you could get your index.html to use some server-side processing, take the email parameter and insert it into the field when you generate the form. But it's easy enough to do this with JavaScript too. – Rup Commented Jul 9, 2012 at 10:46
  • See this question to extract the query string values. Some of the solutions may be a bit over-the-top for what you need but they're robust too. – Rup Commented Jul 9, 2012 at 10:51
Add a ment  | 

3 Answers 3

Reset to default 3
document.getElementById('Id').value='new value';

answer to ment: try this:

<input type="" name="email" id="email">

<iframe id="frm" name="[email protected]" src="[email protected]">
</iframe>

<script>
document.getElementById('email').value = document.getElementById('frm').name;
</script>

then you can change adopt it. change type to hidden and etc.

If your script is executed from the document inside the iframe, you can do this :

var getUrlParameter = function(name, defaultValue) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( document.location.href );
    if( results == null ) return defaultValue;
    else return decodeURIComponent(results[1]);
};

document.getElementById('email').value=getUrlParameter('email');

As you've labeled this question as jQuery You can use $('#Id').val('new value'); But don't forget to add jQuery Library.

发布评论

评论列表(0)

  1. 暂无评论