简体   繁体   English

如何在输入元素的值属性中分配变量?

[英]How to assign variable to value attribute in input element?

I need to assign variable to value attribute in the input element 我需要在input元素中将变量分配给value属性

Here is my input tag. 这是我的输入标签。

<input type="text" id="Name" class="form-control form-control-alternative" placeholder="Usernam" value="myValue">

Here is my variable. 这是我的变量。

var myValue = document.getElementById('userVal');

Anyone, please help me to fix this problem? 有人,请帮助我解决此问题?

You can set input default value. 您可以设置输入默认值。 You can not bind myValue in the html without some js framework. 没有一些js框架,您将无法在HTML中绑定myValue。 To get input value use change event. 要获取输入值,请使用change事件。 Check my code snippet. 检查我的代码段。

 var input = document.getElementById('Name'); var myValue = 'Name example'; input.value = myValue; var myFunction = function (e) { console.log(e.target.value); } 
 <input type="text" id="Name" class="form-control form-control-alternative" placeholder="Usernam" onchange="myFunction(event)" value="myValue"> 

ASSIGN INPUT VALUE TO VARIABLE: 将输入值分配给变量:

You need to assign the variable to the element's value, not the element itself. 您需要将变量分配给元素的值,而不是元素本身。 Also, your current input id is Name , not userVal . 另外,您当前的输入ID是Name ,而不是userVal Change that to userVal then retrieve the value like this: 将其更改为userVal然后像这样检索值:

var myValue = document.getElementById('userVal').value;

Check the following Code Snippet for a practical example on how to retrieve an input value and assign it to a variable: 查看以下代码片段 ,获取有关如何检索输入值并将其分配给变量的实际示例:

 /* JavaScript */ document.querySelector("button").addEventListener("click", function() { var myValue = document.getElementById('userVal').value; alert(myValue); }) 
 <!-- HTML --> <input type="text" id="userVal" class="form-control form-control-alternative" placeholder="Username"> <button>Check Value</button> 

ASSIGN VARIABLE TO INPUT VALUE: 分配可变的输入值:

To assign your input element's value to a variable, just reverse the above assignment like this: 要将输入元素的值分配给变量,只需反转上述分配即可,如下所示:

var newValue = newValue;
document.getElementById('userVal').value = newValue;

Check the following Code Snippet for a practical example on how to assign a variable to your input element's value attribute: 查看以下代码片段,获取有关如何为输入元素的value属性分配变量的实际示例:

 /* JavaScript */ document.querySelector("button").addEventListener("click", function() { var newValue = "newValue"; document.getElementById('userVal').value = newValue; }); 
 <!-- HTML --> <input type="text" id="userVal" class="form-control form-control-alternative" placeholder="Original" value="myValue"> <br /><br /> <button>Change Value</button> 

if you want to assign value to this input: 如果要为该输入分配值:

<input type="text" id="Name" class="form-control form-control-alternative" placeholder="Usernam" value="myValue">

you should use this code: 您应该使用以下代码:

var myValue = document.getElementById('userVal').value;
document.getElementById("Name").value = myValue;

You can look to the documentation and example here: https://www.w3schools.com/jsref/prop_text_value.asp 您可以在此处查看文档和示例: https : //www.w3schools.com/jsref/prop_text_value.asp

In JavaScript, add value property to your code as: 在JavaScript中,将value属性添加到代码中的方式如下:

    var myValue = document.getElementById("name").value

In HTML, use the same id to refer the input tag as: 在HTML中,使用相同的ID来引用输入标签,如下所示:

    <input type="text" id="Name" class="form-control form-control-alternative" placeholder="Usernam" value="myValue">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM