jQuery .change()仅在第一个更改时触发 [英] jQuery .change() only fires on the first change

查看:209
本文介绍了jQuery .change()仅在第一个更改时触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

$.each (t.config.industry, function (i, v) {
    $(v).change(function () {
        console.log("Industry change!");
    });
});

t.config.industry<select> id的数组. t.config.industry: ["#W2549_Sl", "#W2601_Sl", "#W2654_Sl"]我在页面上有几个<select>框,我正在跟踪它们.每当他们任何改变时,我都想开火.

t.config.industry is an array of <select> ids. t.config.industry: ["#W2549_Sl", "#W2601_Sl", "#W2654_Sl"] I have several <select> boxes on the page and I'm tracking them all. Any time any of them change I want to fire something.

很明显,我想做的不仅仅是简单地console.log,而是为了这篇文章,我将其限制于此.在我的网站上,console.log仅在第一次更改时打印一次.在<select>下拉菜单的后续更改中,它不会触发.

Obviously I'm trying to do more than simply console.log, but for the sake of this post I'll limit it to that. On my website the console.log only prints once, on the first change. On subsequent changes of the <select> dropdown menu it does not fire.

有人看过吗?

注意:我根本无法更改HTML标记.

NOTE: I cannot change the HTML mark-up at all.

推荐答案

当多个选择元素的ID不唯一时,我已经见证了这种行为.为了解决我的问题,我简单地删除了id,定义了一个类,并修改了jQuery选择器以使用指定的类.例如:

I've witnessed this behavior when the ids of the multiple select elements weren't unique. To resolve the issue in my case, I simply removed the ids, defined a class, and modified the jQuery selector to use the specified class. For example:

    <select class="some_value" some_id="<%= some[:id] %>">

    $(".some_value").change(function() {
        var some_id = $(this).attr("some_id");
        alert('some_id: ' + some_id);
    });

坦率地说,我认为该行为是jQuery中的错误.为什么它应该成功工作一次,然后失败?

Frankly I consider the behavior a bug in jQuery. Why should it work successfully once and fail thereafter?

这篇关于jQuery .change()仅在第一个更改时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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