简体   繁体   English

jQuery-autoNumeric插件,从.autoNumeric('set',value)自动触发.change()

[英]jQuery - autoNumeric plugin, fire .change() automatically from .autoNumeric('set', value)

I'm using the autoNumeric jQuery plugin . 我正在使用autoNumeric jQuery插件 I want to have the .autoNumeric('set', value) method call the .change() event automatically. 我想让.autoNumeric('set', value)方法自动调用.change()事件。

I have the following code: 我有以下代码:

$(document).ready(function ($) {

    $("#baseCost").change(function() {
        var total = ...; // calculation removed for code brevity
        $("#totalCost").autoNumeric('set', total);
    });

    $("#totalCost").change(function() {
        // this code does not fire when the set above is called
    });

});

How would I accomplish this? 我将如何完成?

Update: In the autoNumeric.js file, I found this (within set ): 更新:在autoNumeric.js文件中,我找到了这个(在set内):

if ($input) {
    return $this.val(value);
}

Given that in my case $input is set to true, I don't understand why this .val() is not firing the .change() on my page. 鉴于在我的情况下$ input设置为true,所以我不明白为什么此.val()没有在页面上触发.change()

I did not realize that .val() does not trigger .change() by default. 我没有意识到默认情况下.val()不会触发.change() For my question specifally, the following does the trick in autoNumeric.js: 对于我的问题,以下是autoNumeric.js中的技巧:

if ($input) {
    if ($this.val(value)) {
        return $this.trigger("change");
    }
    return false;
}

For more information, see this answer . 有关更多信息,请参见此答案

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

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