更改事件是否在文档就绪时触发? [英] Does the change event fire on documentready?

查看:94
本文介绍了更改事件是否在文档就绪时触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框.页面加载时,第一个文本框的值将应用于第二个文本框.

I have two text boxes. When the page loads, the value of the first text box is applied to the second.

我需要在页面加载时触发change事件.

I need the change event to fire when the page loads.

HTML:

a: <input type="text" id="a" value="x" />
b: <input type="text" id="b" value="y" />

JQuery:

// Apply value of #a to #b and fire change event...
$('#b').val($('#a').val()).change();

// Listen for the change and alert...
$('#b').on('change', function() {
    alert("Change event fired on load.");
});

JSFiddle:

为了您的方便: http://jsfiddle.net/clarusdignus/rHYLM/

更改事件未触发.

推荐答案

在事件被代码触发后附加事件处理程序

The event handlers are attached after the event is fired by code

第一个应该是

  // Listen for the change and alert...
$('#b').on('change', function() {
    alert("Change event fired on load.");
});

// Apply value of #a to #b and fire change event...
$('#b').val($('#a').val()).change();

第二个应该是

// Listen for the change and alert...
$('#b').on('change', function() {
    alert("Change event fired on load.");
});

// Apply value of #a to #b and fire change event...
$('#b').val($('#a').val()).trigger('change');

这篇关于更改事件是否在文档就绪时触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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