将输入值设置为Javascript变量 [英] Set Input Value to Javascript Variable

查看:101
本文介绍了将输入值设置为Javascript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在javascript中有一个名为x的变量.如何将文本输入(HTML)的值设置为该变量?例如:

Let's say I have a variable called x in javascript. How can I set the value of a text input (HTML) to that variable? For example:

现在输入的值将为Swag

The value of the input will now be Swag

<input type="text" value="Swag" />

但是,如果我希望该值是一个javascript变量?我该怎么办?像这样吗? (我只是在猜测,试图阐明我的观点)

But if I want the value to be a javascript variable? How do I do? Something like this? (I am just guessing, trying to make my point clear)

<input type="text" value="variable.x" />

推荐答案

您可以在javascript代码中进行设置,例如:

You can set it in your javascript code like:

<input id="myInput" type="text" value="Swag" />

<script>
    var test = "test";
    document.getElementById("myInput").value = test;
</script>

这篇关于将输入值设置为Javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆