jQuery onchange 检测

发布于 2024-10-24 04:53:36 字数 382 浏览 8 评论 0原文

我想执行 html 元素的“onchange”属性中的 javascript。所以..

<input id="el" type="text" onchange="alert('test');" value="" />

使用该示例,我想通过 jQuery 执行 alert('test'); 部分,问题是 .change() 事件处理程序不是不起作用,因为我想在另一个元素更改其值后执行它。所以..

$('#el').val('test');

这是我想执行onchange的时候。之后 .val val 被调用。有什么想法吗?

I want to execute javascript that's in the "onchange" attribute of an html element. So..

<input id="el" type="text" onchange="alert('test');" value="" />

Using that example I'd like to execute the alert('test'); portion via jQuery, the problem is the .change() event handler isn't working, because I want to execute it after another element changes it's value. So..

$('#el').val('test');

This is when I'd like to execute the onchange. After that .val val has been called. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

屋顶上的小猫咪 2024-10-31 04:53:36

.val() 不会触发 change 事件。您需要自己手动执行此操作。例如:

$('#el').val('test').change();

您可以将其封装在一个小的 jQuery 插件中,如下所示:

(function ($) {
    $.fn.changeVal = function (text) {
        this.val(text).change();
    };
}(jQuery));

$('#el').changeVal('test');

.val() does not trigger change events. You need to do so yourself, manually. For example:

$('#el').val('test').change();

You can encapsulate this in a small jQuery plugin like so:

(function ($) {
    $.fn.changeVal = function (text) {
        this.val(text).change();
    };
}(jQuery));

$('#el').changeVal('test');
一片旧的回忆 2024-10-31 04:53:36

我认为当字段值发生变化时 onchange 事件总是触发是有道理的。因此,我会更改 JQuery 本身中的 val 函数,而不是创建自定义函数。这样,您就不必担心使用哪个函数来设置对象的值,并且如果您已经使用过 JQuery,则不必更改所有 val 调用编写您的应用程序。这是代码:

//Store the old val function
$.fn.custom_oldVal = $.fn.val;

//Update the val function to fire the change event where appropriate:
$.fn.val = function(value) {
    if(value == null || value == undefined){
        return this.custom_oldVal();
    } else {
        //Only call onchange if the value has changed
        if(value == this.custom_oldVal()){
            return this;//Return this object for chain processing
        } else {
            return this.custom_oldVal(value).change();
        }
    }
}

I think it makes sense that the onchange event ALWAYS fires when the value of the field changes. Thus, I would change the val function in JQuery itself rather than make a custom function. That way, you don't have to worry about which function you use to set the value of an object, and don't have to go change all of your val calls if you've already used JQuery to code your app. Here's the code:

//Store the old val function
$.fn.custom_oldVal = $.fn.val;

//Update the val function to fire the change event where appropriate:
$.fn.val = function(value) {
    if(value == null || value == undefined){
        return this.custom_oldVal();
    } else {
        //Only call onchange if the value has changed
        if(value == this.custom_oldVal()){
            return this;//Return this object for chain processing
        } else {
            return this.custom_oldVal(value).change();
        }
    }
}
阳光的暖冬 2024-10-31 04:53:36

不久前我也遇到了同样的问题,但找不到任何好的解决方案。似乎最好的解决方案是对输入进行“拉动”,或者从实际更改输入内容的代码中触发 onchange 函数(如 Domenic 所示)。

I had the same problem a while ago, but couldn't find any good solutions to it. It seems the best solution is to do a "pull" on the input, or to fire your onchange function (like Domenic shows) from the code that actually changes the content of the input.

一杯敬自由 2024-10-31 04:53:36

如果你先
input.focus(); 然后 $.val('abcd'); 将触发更改事件。
取自 jquery 站点

if you first
input.focus(); then $.val('abcd'); a change event will be fired.
taken from jquery site.

怎樣才叫好 2024-10-31 04:53:36

我会重新设计并开始使用 knockout.js 插件,该插件使用数据绑定,让您可以轻松使用所有绑定。

示例代码如下:

var ViewModel = function(first, last) {
this.firstName = ko.observable(first);
this.lastName = ko.observable(last);

this.fullName = ko.computed(function() {
    // Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.
    return this.firstName() + " " + this.lastName();
}, this);

};

ko.applyBindings(new ViewModel("行星", "地球"));

I Would redesign and start using knockout.js plugin which is using data-bind where let u use all the binding pretty easy.

sample code below:

var ViewModel = function(first, last) {
this.firstName = ko.observable(first);
this.lastName = ko.observable(last);

this.fullName = ko.computed(function() {
    // Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.
    return this.firstName() + " " + this.lastName();
}, this);

};

ko.applyBindings(new ViewModel("Planet", "Earth"));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文