Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果输入不是来自键入,为什么输入类型文本内的onChange不起作用_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如果输入不是来自键入,为什么输入类型文本内的onChange不起作用

Javascript 如果输入不是来自键入,为什么输入类型文本内的onChange不起作用,javascript,jquery,html,Javascript,Jquery,Html,我有两个文本字段,如下所示: HTML: <input type="text" id="input1" onchange="doSomething();" disabled/> <input type="text" id="input2"/> function doSomething(){ alert('Changed!'); } $("#input2").change(function(){ $("#input1").val(this.val()); });

我有两个文本字段,如下所示:

HTML:

<input type="text" id="input1" onchange="doSomething();" disabled/>
<input type="text" id="input2"/>
function doSomething(){
 alert('Changed!');
}

$("#input2").change(function(){
 $("#input1").val(this.val());
});
我的问题是:

<input type="text" id="input1" onchange="doSomething();" disabled/>
<input type="text" id="input2"/>
function doSomething(){
 alert('Changed!');
}

$("#input2").change(function(){
 $("#input1").val(this.val());
});
当我改变input2的值时,我希望input1会做一些事情 原因值已更改。但我的问题不是我的问题 马上有人能解释为什么以及如何修复它吗

您应该使用
$(this.val()
this.value
val()
是jquery方法,所以您需要使用jquery
$(this)
选择器

$(“#输入2”).keyup(函数(){
$(“#input1”).val($(this.val());
});

您的代码运行良好,完美地回答了我的问题。非常感谢。