JSFIDDLE
Html Code:
<body>
<input value="123">
<button>button</button>
</body>
Js Code(using JQuery 1.2.0):
$(function(){
$("button").click(function(){
$("input").val("balabala...");
});
$("body").on("change","input",function(){
alert("change");
alert($("input").val());
});
});
Question:
At the beginning, the value of the input is "123".
When I change the text through editing by hands and click other places not the input field, the change event of the input will trigger.
However, when I click the button, which will also change the value of the input, the change event of the input will not work.
Thanks for any help.
The change
event is not triggered when changing the value programatically with code, you have to trigger it yourself
$(function(){
$("button").click(function(){
$("input").val("balabala...").trigger('change');
});
$("body").on("change","input",function(){
alert("change");
alert($("input").val());
});
});
FIDDLE
JSFiddle
You need to append .change()
onto the click
function:
$(function(){
$("button").click(function(){
$("input").val("balabala...").change();
});
$("body").on("change","input",function(){
alert("change");
alert($("input").val());
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With