jquery .live to .on抑制弹出窗口

时间:2014-04-11 11:04:26

标签: jquery ajax

我目前正在将网站升级到新的jquery版本,因此我需要使用折旧的.live事件更改脚本。我已经转换代码似乎成功,因为它有点工作但是当返回html数据时,我得到一个带有html代码的弹出窗口,并且必须单击一个OK按钮。然后将返回的数据注入页面中。我的问题是如何抑制/隐藏此弹出窗口以便html,这样用户就不必单击“确定”按钮。

 $(function() {
var image = "<img src='template-data/loading.gif' alt='loading' id='basketLoadingImg' style='margin-left:20px;'>";
// error message when shipping table fails to update
var errorShippingTable = "Error updating the shipping table. Please refresh the page.";
function postcode() {
    var val = $('input:text[name=postcode]').val();
    if (val == '') {$('input:text[name=postcode]').focus(); return true;}
    var dataString = '_a=setPostCode&' + 'postcode='+ val + '&basket=' + $('input:hidden[name=basket]').val();
    $.ajax({type: 'POST', url: 'basket.do', data: dataString,
        beforeSend: function () {
            $("#submitPostcode").after(image);
        },
        success:
        function(data) {
            try {
                $("#shippingTable").html($(data).find("#shippingTable").html());
            } catch (err) {alert(errorShippingTable);}
            try {
                $("p.checkout").html($(data).find("p.checkout").html());
            } catch (err) {alert('Error: No shipping checkout button found');}
            $("#basketLoadingImg").remove();
        }
    }); return false;
}
function voucher() {
    var val = $('input:text[name=voucherCode]').val();
    if (val === '') {$('input:text[name=voucherCode]').focus(); return true;}

    var dataString = '_a=addVoucherAjax&' + 'voucherCode='+ val + '&basket=' + $('input:hidden[name=basket]').val();
    $.ajax({type: 'POST', url: 'basket.do', data: dataString,
        beforeSend: function () {
            $("#voucherAddButton").after(image);
        },
        success:
        function(data) {
            updateBasketPage(data);
            try {
                if ($(data).find("td.voucher").html() != null)
                    $("td.voucher").html($(data).find("td.voucher").html());
            } catch (err) {
                alert('Error updating the page. Please refresh it.');
            }
            $("#basketLoadingImg").remove();
        },
        error: function(data) {
            alert('Error while adding the voucher');
            $("#basketLoadingImg").remove();
        },
        dataType: 'html'
    }); return false;
}
function updateBasketPage(data) {
    try {
        if ($(data).find("#shippingTable").html() != null)
            $("#shippingTable").html($(data).find("#shippingTable").html());
    } catch (err) {alert(errorShippingTable);}
    try {
        if ($(data).find("td.price").html() != null)
            $("td.price").html($(data).find("td.price").html());
    } catch (err) { }
    try {
        if ($(data).find("#head_bask").html() != null)
            $("#head_bask").html($(data).find("#head_bask").html());
        else alert(data);
    } catch (err) { }
    try {
        if ($(data).find("p.checkout").html() != null)
            $("p.checkout").html($(data).find("p.checkout").html());
    } catch (err) {alert('Error updating the proceed to checkout button. Please refresh the page.');}
}
$('table').on('click', 'input#submitPostcode', function() {
    return postcode();
});
$('table').on('click', 'input#submitPostcode', function() {
    return postcode();
});
$('table').on('keydown', 'input:text[name=postcode]', function(e) {
    if (e.which == 13) {
        return postcode();
    }
    return true;
});
$('table').on('change', 'input:radio[name=shippingMethod]', function() {
    var dataString = '_a=setShippingMethod' + '&shippingMethod=' + $('input:radio[name=shippingMethod]:checked').val() + '&basket=' + $('input:hidden[name=basket]').val();

    $.ajax({type: 'POST', url: 'basket.do', data: dataString,
        beforeSend: function () {
            $("#deliveryMethod th:first").append(image);
            if ($('input:radio[name=shippingMethod]:checked').val() != 'SpeedcreteDelivery') {
                $("#shippingTable").hide('blind', 1000);
            }
        },
        success:
        function(data) {
            updateBasketPage(data);
            if ($('input:radio[name=shippingMethod]:checked').val() != 'SpeedcreteDelivery') {
                $("#shippingTable").show('blind', 1000);
            }
            $("#basketLoadingImg").remove();
        },
        error:
        function() {
            alert('Error while updating the shipping method');
            $("#shippingTable").show('blind', 1000);
            $("#basketLoadingImg").remove();
        }
    });
    return false;
});
$('table').on('change', 'input:radio[name=branch]', function() {
    var dataString = '_a=setBranch' + '&branch=' + $('input:radio[name=branch]:checked').val() + '&basket=' + $('input:hidden[name=basket]').val();

    $.ajax({type: 'POST', url: 'basket.do', data: dataString,
        beforeSend: function () {
            $("#deliveryMethod th:first").append(image);
        },
        success:
        function(data) {
            updateBasketPage(data);
            $("#shippingTable").fadeIn('slow');
            $("#basketLoadingImg").remove();
        },
        error: function() {
            alert('Error while setting the collection');
            $("#basketLoadingImg").remove();
        }
    });
    return false;
});
$('table').on('change', 'input:radio[name=deliveryOption]', function() {
    var dataString = '_a=setDeliveryOption' + '&deliveryOption=' + $('input:radio[name=deliveryOption]:checked').val() + '&basket='
        + $('input:hidden[name=basket]').val();

    $.ajax({type: 'POST', url: 'basket.do', data: dataString,
        beforeSend: function () {
            $("#deliveryMethod th:first").append(image);
        },
        success:
        function(data) {
            updateBasketPage(data);
            $("#shippingTable").fadeIn('slow');
            $("#basketLoadingImg").remove();
        },
        error: function() {
            alert('Error while setting the delivery option');
            $("#basketLoadingImg").remove();
        }
    });
    return false;
});
$('table').on('click', 'a#removeVoucherButton', function() {
    var dataString = '_a=removeVoucher' + '&basket=' + $('input:hidden[name=basket]').val();

    $.ajax({type: 'POST', url: 'basket.do', data: dataString,
        beforeSend: function () {
            $("#removeVoucherButton").html(image);
        },
        success:
        function(data) {
            updateBasketPage(data);
            try {
                if ($(data).find("td.voucher").html() != null)
                    $("td.voucher").html($(data).find("td.voucher").html());
            } catch (err) {
                alert('Error: No voucher form found');
            }
            $("#basketLoadingImg").remove();
        },
        error: function() {
            alert('Error while removing the voucher');
            $("#basketLoadingImg").remove();
        },
        dataType: 'html'
    });
    return false;
});
    $('table').on('click', '#voucherAddButton', function() {
        return voucher();
    });
    $('table').on('keydown', 'input:text[name=voucherCode]', function(e) {
        if (e.which == 13) {
            return voucher();
        }
        return true;
    });
});  

0 个答案:

没有答案