Jquery script works in all browsers except IE8 and lower
I am running an ecommerce shop and the following script is used to add an item to the cart without a page reload. It works in all major browsers including IE9 but it just does the default page reload with IE8 and below. No errors show but it reloads the page which it IS NOT supposed to do. Here is the code-
$(document).ready(function() {
$('#cart_quantity').live('submit', function() {
$('#button_submit').attr('disabled', 'disabled');
var options = {};
options = { to: "#ajax_cart", className: 'ui-effects-transfer' };
$("#button_add_cart").effect('transfer',options,1300);
var datas_form = $('#cart_quantity').serializeArray();
$.ajax({
url:'ajax_add_cart.php',
data: datas_form,
type: 'POST',
success: function(data) {
var datas = data.split("|");
setTimeout(function() {
$('#content_products').html(datas[0]);
$('#content_total').html(datas[1]);
$('#fila_' + datas[2]).fadeOut("fast").fadeIn("fast").fadeOut("fast").fadeIn("fast").fadeOut("fast").fadeI开发者_如何学Pythonn("fast");
$('#content_total').fadeOut("fast").fadeIn("fast").fadeOut("fast").fadeIn("fast").fadeOut("fast").fadeIn("fast");
}, 1300);
}
});
$('#button_submit').attr('disabled', '');
return false;
});
});
Two tips:
- add
e.preventDefault()
to the beginning of the handler. Any error in the handler will result inreturn false
not executing and the browser performing the default action unlesspreventDefault
was called before the error. - avoid live submit handlers, they are simulated in old IE via the click handler (as submit events do not bubble), which can lead to various perks (see e.g. bug 7061).
try adding an event.preventDefault()
to the submit function
source: http://api.jquery.com/event.preventDefault/
精彩评论