Javascript 将javascript变量传递给html文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4252130/
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
Passing javascript variable to html textbox
提问by Dayzza
i need help on html form.
我需要有关 html 表单的帮助。
I got a javascript variable, and I am trying to pass the variable to a html form texbox. I want to display the variable on the textbox dynamically. but i do not know how to pass the variable to the html form and call the variable?
我有一个 javascript 变量,我正在尝试将该变量传递给一个 html 表单 texbox。我想在文本框上动态显示变量。但我不知道如何将变量传递给 html 表单并调用该变量?
var test;
<INPUT TYPE="TEXT" NAME="lg" VALUE="" SIZE="25" MAXLENGTH="50" disabled="disabled"><BR><BR>
How do i pass test to html form and change its value?
如何将测试传递给 html 表单并更改其值?
Thanks
谢谢
回答by John Hartsock
Pass the variable to the form element like this
像这样将变量传递给表单元素
your form element
你的表单元素
<input type="text" id="mytext">
javascript
javascript
var test = "Hello";
document.getElementById("mytext").value = test;//Now you get the js variable inside your form element
回答by David
<form name="input" action="some.php" method="post">
<input type="text" name="user" id="mytext">
<input type="submit" value="Submit">
</form>
<script>
var w = someValue;
document.getElementById("mytext").value = w;
</script>
//php on some.php page
echo $_POST['user'];
回答by Nikhil Patel
instead of
代替
document.getElementById("txtBillingGroupName").value = groupName;
You can use
您可以使用
$("#txtBillingGroupName").val(groupName);
instead of groupName you can pass string value like "Group1"
您可以传递字符串值而不是 groupName,例如“Group1”
回答by Peter Wray
You could also use to localStorage feature of HTML5 to store your testvalue and then access it at any other point in your website by using the localStorage.getItem()
method. To see how this works you should look at the w3schoolsexplanation or the explanation from the Opera Developer website. Hope this helps.
您还可以使用 HTML5 的 localStorage 功能来存储您的测试值,然后使用该localStorage.getItem()
方法在您网站的任何其他点访问它。要了解这是如何工作的,您应该查看w3schools 的解释或来自Opera 开发人员网站的解释。希望这可以帮助。
回答by gavin
This was a problem for me, too. One reason for doing this (in my case) was that I needed to convert a client-side event (a javascript variable being modified) to a server-side variable (for that variable to be used in php). Hence populating a form with a javascript variable (eg a sessionStorage key/value) and converting it to a $_POST variable.
这对我来说也是一个问题。这样做的一个原因(就我而言)是我需要将客户端事件(正在修改的 javascript 变量)转换为服务器端变量(以便在 php 中使用该变量)。因此,使用 javascript 变量(例如 sessionStorage 键/值)填充表单并将其转换为 $_POST 变量。
<form name='formName'>
<input name='inputName'>
</form>
<script>
document.formName.inputName.value=var
</script>
回答by vibol rim
document.getElementById("txtBillingGroupName").value = groupName;