简体   繁体   English

如果同一类的任何文本输入中的值发生变化,如何调用函数?

[英]How to call function if the value changes in any text input with same class?

How to call function if another function changes the value of any text field with class netvalue. 如果另一个函数更改类netvalue的任何文本字段的值,如何调用函数。

<input name="1" type="text" class="netvalue" readonly>
<input name="2" type="text" class="netvalue" readonly>
<input name="3" type="text" class="netvalue" readonly>

<script>
$(".netvalue").oninput(function(){
       confirm('changed');   
});     
<script>

You can trigger an input-change. 您可以触发输入更改。

 $(".netvalue").change(function(){ console.log('changed') }); $("#button").click(function(){ $("input[name='1']").val("some text"); $(".netvalue").trigger('change'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="button">Insert text</button> <input name="1" type="text" class="netvalue" > <input name="2" type="text" class="netvalue" > <input name="3" type="text" class="netvalue" > 

you have to trigger the change function inside the function that is changing the value of the input. 您必须在正在更改输入值的函数内触发change函数。

see below 见下文

 var input = $(".netvalue"), but = $("button") $(but).on("click",function() { $(input).val("1").trigger("change") }) $(input).on("change",function() { alert("changed") }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input name="1" type="text" class="netvalue" readonly> <input name="2" type="text" class="netvalue" readonly> <input name="3" type="text" class="netvalue" readonly> <button> clickME </button> 

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

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