第一次单击复选框时 jquery live 不会触发

发布于 2024-10-08 08:00:11 字数 1046 浏览 4 评论 0原文

我有一个奇怪的问题。此代码在 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 技术交流群。

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

发布评论

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

评论(3

快乐很简单 2024-10-15 08:00:11

在复选框失去焦点之前,更改事件不会在 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.

无人问我粥可暖 2024-10-15 08:00:11

我发现更改会导致 IE 出现一些问题。尝试改用点击事件。这似乎可以解决问题。

I have found that change causes some problems in IE. Try using the click event instead. This appears to fix the problem.

你的呼吸 2024-10-15 08:00:11

我遇到了类似的问题,并通过在页面加载时调用 .change() 来解决它。

$(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
    }
}).change();
});

I had a similar problem and solved it by calling .change() once on page load.

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