简体   繁体   English

使用javascript设置输入类型文本的值

[英]Using javascript to set value of input type text

i can't figure out how to set a value using javascript. 我无法弄清楚如何使用javascript设置值。

Here is how the HTML look like: 以下是HTML的外观:

<input type=​"text" name=​"cardholderName" data-bind=​"value:​ creditCardForm.name, 
css:​    creditCardForm.style.name" maxlength=​"30">​

When i try to set the value in the Chrome console 当我尝试在Chrome控制台中设置值时

document.getElementsByName('cardholderName').value="myname";

The console returns "myname" but the field in the browser isn't populated with this approach. 控制台返回“myname”,但浏览器中的字段未填充此方法。 Any hints on what is missing? 什么是缺失的暗示? Thanks 谢谢

getElementsByName returns an array, and there is no property value on an array. getElementsByName返回一个数组,并且数组上没有属性value I think you mean to get the first item: 我想你的意思是获得第一项:

document.getElementsByName('cardholderName')[0].value="myname";

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

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