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

时间:2010-12-16 19:48:21

标签: asp.net-mvc-2 checkbox cross-browser jquery

我有一个奇怪的问题。此代码在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 live event wireup,简化

$(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切换到兼容模式,则直播活动永远不会起作用。点击链接的其他直播活动也很合适。

3 个答案:

答案 0 :(得分:3)

在复选框失去焦点之前,更改事件在IE中无法正确触发。

错误:http://webbugtrack.blogspot.com/2007/11/bug-193-onchange-does-not-fire-properly.html

您需要改为映射到“点击”事件。

答案 1 :(得分:1)

我发现改变会导致IE中出现一些问题。请尝试使用click事件。这似乎解决了这个问题。

答案 2 :(得分:0)

我有一个类似的问题,并通过在页面加载时调用.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();
});