2011-10-13 84 views
0

你怎么把一个javascript变量为<input>箱的value=""<form>HTML和JavaScript变量

这必须是容易的,但我不知道。

+0

可能重复的[设置与javascript中的输入字段的值](http://stackoverflow.com/questions/7609130/set-the-value-of-a-input-field-with-javascript) –

回答

1

这并不难。看看下面的例子:

HTML:

<input id="id_of_your_element"> 

的Javascript:

var yourvariable = "Hello world!"; 
document.getElementById("id_of_your_element").value = yourvariable; 
1
document.getElementById('element-id').value = 'The Value'; 
0
Value="<script type="text/javascript">document.write(yourVariableName);</script>" 
+0

你应该避免使用document.write,尤其是对于DOM方法这样一个简单的应用程序可用 - http://stackoverflow.com/questions/802854/why-is-d ocument写考虑的,一个坏实践 – nickb

0

你可以这样做:

document.getElementById('inputId').value = yourVariable; 

您还可以使用jQuery库,它会:

$('#inputId').val(yourVariable);