如何将外部JavaScript变量传递给HTML表单 [英] How to pass Variable from External JavaScript to HTML Form
问题描述
我一直试图将外部JavaScript文件的值传递给HTML表单,但没有运气。这些文件相当大,所以我不确定我可以解释这一切,但生病尝试。
基本上,用户点击一个链接,然后启动一个js文件。
我需要在表单字段中将此值传递给HTML页面。
Javascript:
var divElement = function(){
divCode = document.getElementById(div1).innerHTML;
返回divCode; };
document.getElementById('adcode')。value = divElement();
之后它应该被传递给这个Form字段
HTML表单字段:
< p>广告代码:< br>
< input type =textname =adcodeid =adcode/>
< br>
< / p>
感谢您的帮助!
您的HTML文件需要引用JavaScript js
文件。在你的JavaScript中有一个函数返回你需要的值。使用JavaScript(我喜欢jQuery)将表单字段设置为所需内容。
JS文件:
<脚本>
var divElement = function(){
divCode = document.getElementById(div1).innerHTML;
返回divCode; };
document.getElementById('adcode')。value = divElement();
函数GetDivElement(){
return divElement();
}
< / script>
HTML档案:
< p>广告代码:
< br />
< input type =textname =adcodeid =adcode/>
< br />
< / p>
< script src =无论那个js文件是/>
< script>
window.onload = function(){
document.getElementById('adcode')。value = GetDivElement();
}
< / script>
虽然,这可能会做你想做的事情(取决于你想做什么):
< p>广告代码:
< br />
< input type =textname =adcodeid =adcode/>
< br />
< / p>
< script src =无论那个js文件是/>
< script>
window.onload = function(){
GetDivElement();
}
< / script>
I have been trying to pass a value from an external javascript file to an HTML form with no luck. The files are rather large so I am not sure I can explain it all but ill try.
Basically a user clicks a link then a js file is initiated. Immediately after a new HTML page loads.
I need this value passed to the HTML page in a form field.
Javascript:
var divElement = function(){
divCode = document.getElementById(div1).innerHTML;
return divCode; };
document.getElementById('adcode').value = divElement();
Afterwards it should be passed to this Form field
HTML Form Field:
<p>Ad Code:<br>
<input type="text" name="adcode" id="adcode"/>
<br>
</p>
Thanks for any help!
Your HTML file needs to reference the JavaScript js
file. Have a function in your JavaScript that returns the value that you need. Use JavaScript (I like jQuery) to set the form field to what you need.
JS file:
<script>
var divElement = function(){
divCode = document.getElementById(div1).innerHTML;
return divCode; };
document.getElementById('adcode').value = divElement();
function GetDivElement() {
return divElement();
}
</script>
HTML file:
<p>Ad Code:
<br />
<input type="text" name="adcode" id="adcode"/>
<br />
</p>
<script src="wherever that js file is" />
<script>
window.onload = function() {
document.getElementById('adcode').value = GetDivElement();
}
</script>
Although, really, this might do what you want (depending on what you are trying to do):
<p>Ad Code:
<br />
<input type="text" name="adcode" id="adcode"/>
<br />
</p>
<script src="wherever that js file is" />
<script>
window.onload = function() {
GetDivElement();
}
</script>
这篇关于如何将外部JavaScript变量传递给HTML表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!