为什么输入值的确发生了变化,但jquery change事件却没有触发? [英] why does the jquery change event not trigger though the value of input changed indeedly?

查看:191
本文介绍了为什么输入值的确发生了变化,但jquery change事件却没有触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSFIDDLE

HTML代码:

JSFIDDLE

Html Code:

<body>
    <input value="123">
    <button>button</button>
</body>

Js代码(使用JQuery 1.2.0):

Js Code(using JQuery 1.2.0):

$(function(){
    $("button").click(function(){
        $("input").val("balabala...");
    });

    $("body").on("change","input",function(){
        alert("change");
        alert($("input").val());
    });
});

问题:
开始时,输入值为"123".

当我通过手动编辑更改文本并单击其他地方而不是输入字段时,将触发输入的change事件.

但是,当我单击也会更改输入值的按钮时,输入的change事件将不起作用.

感谢您的帮助.

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.

推荐答案

使用代码以编程方式更改值时,不会触发change事件,您必须自行触发

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

这篇关于为什么输入值的确发生了变化,但jquery change事件却没有触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆