Javascript 更改输入值 onchange?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5457739/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:16:45  来源:igfitidea点击:

Change value of input onchange?

javascripthtml

提问by Jay Wit

I want a very simply Javascript function (which I can't get to work, not so simple for me).
When Someone inserts a number in an inputfield, the value of an other inputfield should change to that value. I have atm:

我想要一个非常简单的 Javascript 函数(我无法开始工作,对我来说不是那么简单)。
当有人在输入字段中插入数字时,其他输入字段的值应更改为该值。我有提款机:

function updateInput(ish) {  
    fieldname.value = ish;  
}  
<input type="text" name="fieldname" id="fieldname" />  
<input type="text" name="thingy" onchange="updateInput(value)" /> 

But this doesn't work somehow, can someone help me out?
Thanks in advance!

但这以某种方式不起作用,有人可以帮助我吗?
提前致谢!

回答by Alexey Romanov

You can't access your fieldnameas a global variable. Use document.getElementById:

您无法将您fieldname作为全局变量访问。使用document.getElementById

function updateInput(ish){
    document.getElementById("fieldname").value = ish;
}

and

onchange="updateInput(this.value)"

回答by Aditya P Bhatt

for jQuerywe can use below:

因为jQuery我们可以在下面使用:

by input name:

按输入名称:

$('input[name="textboxname"]').val('some value');

by input class:

按输入类:

$('input[type=text].textboxclass').val('some value');

by input id:

通过输入 ID:

$('#textboxid').val('some value');