jQuery change event se llama dos veces


Tengo un formulario con algunos cuadros de entrada y selección, cada uno tiene class="myClass". También tengo el siguiente script:

$(document).ready(function() {
    $(".myClass").change(function() {
        alert('bla');
    })
});

No entiendo por qué después de cada cambio en select box o input box, esta función se llama dos veces. ¿Qué pasa aquí?

Agradezco su ayuda!

Author: Ender, 2011-03-29

7 answers

Todo lo que se me ocurre es que usaste la misma clase en el propio formulario. si es así, elimina el estilo MyClass de tu etiqueta de formulario.

Corregido : http://jsfiddle.net/rY6Gq/1 /

Uno defectuoso con doble alerta: http://jsfiddle.net/rY6Gq /

 39
Author: Marc Uberstein,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-28 20:34:14

E. stopImmediatePropagation(); es lo que funcionó para mí.

$(document).ready(function() {
    $(".myClass").change(function(e) {
        e.stopImmediatePropagation();
        alert('bla');
    })
});
 13
Author: jwebb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-12 00:16:45

Es un error, Usted añadiría

$("#some_id").unbind('change');

Antes de cualquier cambio llame

 7
Author: diego matos - keke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-21 17:17:27

Si esto ocurrió en IE, puede ser este error como lo fue para mí: http://bugs.jquery.com/ticket/6593

Actualizar a jQuery 1.7.1 funcionó para mí.

 3
Author: user996015,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-04 20:21:20

Sucede cuando la misma clase o cualquier atributo que esté enlazando también tiene el mismo nombre padre o hijo. Obviamente, cuando cambias a un hijo, el padre también cambia (su hijo cambia). Si tienen la misma clase o atributo, debería dispararse dos veces. Por ejemplo, en lo siguiente si se enlaza a "MyClass", se llamará dos veces.

<div class="myclass">
<select class="myClass">   </select>
</div>
 3
Author: Selay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-19 03:17:25
 0
Author: Šime Vidas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-28 20:31:36

Intente depurar el código en Firebug en lugar de alertar. Puede estar perdiendo el foco y devolverlo está causando la aparición de dos cambios cuando no hay dos sucediendo

 -1
Author: Scott Reed,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-28 20:31:40