第一次单击复选框时 jquery live 不会触发
我有一个奇怪的问题。此代码在 Chrome 和 Firefox 中运行良好,但在 IE 8 中,当我第一次取消选中某个框时,实时事件不会触发。如果我选中它然后再次取消选中它之后每次都会起作用。
视图中的服务器端代码
<%: Html.CheckBox("select-invoice-" + invoice.InvoiceNumber,
true,
new { title = "choose to not pay anything on this invoice by unchecking this box" }) %>
呈现为以下内容
<input checked="checked" id="select-invoice-TST-1001"
name="select-invoice-TST-1001"
title="choose to not pay anything on this invoice by unchecking this box"
type="checkbox" value="true" />
这是我的 javascript 实时事件连线,经过简化
$(function () {
$("[id^='select-invoice-']").live('change', function () {
var invoiceId = $(this).attr('id').substr('select-invoice-'.length);
ComputeTotalPayment();
if ($(this).is(':checked')) {
//save invoice data
} else {
//remove invoice data
}
});
});
在任何浏览器上的 javascript 中都没有错误。如果我将 IE 切换到兼容模式,则现场活动将永远无法进行。其他点击链接的现场活动效果很好。
I have a strange problem. This code works fine in chrome and firefox, but in IE 8 the live event will not fire the first time I uncheck a box. If I check it and then uncheck again it works every time after that.
My serverside code in the view
<%: Html.CheckBox("select-invoice-" + invoice.InvoiceNumber,
true,
new { title = "choose to not pay anything on this invoice by unchecking this box" }) %>
renders to this
<input checked="checked" id="select-invoice-TST-1001"
name="select-invoice-TST-1001"
title="choose to not pay anything on this invoice by unchecking this box"
type="checkbox" value="true" />
Here is my javascript live event wireup, simplified
$(function () {
$("[id^='select-invoice-']").live('change', function () {
var invoiceId = $(this).attr('id').substr('select-invoice-'.length);
ComputeTotalPayment();
if ($(this).is(':checked')) {
//save invoice data
} else {
//remove invoice data
}
});
});
There are no errors in the javascript on any browser. If I switch IE to compatibility mode the live event never works. Other live events for clicks on links work just fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在复选框失去焦点之前,更改事件不会在 IE 中正确触发。
错误: http://webbugtrack.blogspot .com/2007/11/bug-193-onchange-does-not-fire-properly.html
您需要映射到“click”事件。
The change event doesn't fire correctly in IE until the checkbox loses focus.
Bug: http://webbugtrack.blogspot.com/2007/11/bug-193-onchange-does-not-fire-properly.html
You'll need to map to the "click" event instead.
我发现更改会导致 IE 出现一些问题。尝试改用点击事件。这似乎可以解决问题。
I have found that change causes some problems in IE. Try using the click event instead. This appears to fix the problem.
我遇到了类似的问题,并通过在页面加载时调用 .change() 来解决它。
I had a similar problem and solved it by calling .change() once on page load.