JQuery PreventDefault() 正在停止后续函数
我正在使用 PreventDefault 函数来阻止我的表单提交。这工作正常,但是在该功能之后我有一个不再工作的 onClick 功能。
奇怪的是,在 onclick 函数之前,我确实有一个简单的 div 居中函数,即使遵循 PreventDefault 形式,它也可以工作。
编辑:我发现了问题,onclick 函数适用于第一个函数中通过 ajax 加载的项目。所以选择器可能找不到它
下面是我的代码:
// Submit zip code
$("#get_zip").submit(function (zip)
{
//Send zipcode
$.ajax({
type: "POST",
url: "ajax_calls.php",
data: "zip="+$('#zip').val()+"&send_zip=true",
success: function(listing){$("#wrapper").html(listing); $("#lightbox,#welcome").fadeOut(300);},
error: function(){alert(3);$("#error").text("Could not retrieve posts").fadeIn(300).delay(900).fadeOut(300)}
});
//Stop form submission action
zip.preventDefault();
});
//Center item box
$("#item").centerInClient();
//When clicking an item row
$("tr.item").click(function()
{
// Get item id from selected row id
var id = $(this).attr('id');
//Fill item box with item details
$.ajax({
type: "POST",
url: "ajax_calls.php",
data: "id="+id+"&get_item=true",
success: function(r){$("#item_detail").html(r); $("#lightbox, #item").fadeIn(300);},
error: function(){alert(2);$("#error").text("Could not retrieve posts").fadeIn(300).delay(900).fadeOut(300)}
});
});
任何对此的帮助将非常感激。
非常感谢
I am using the preventDefault function to stop my form from submitting. This is working fine, however following that function I have a onClick function which is no longer working.
Strangely, before the onclick function I do have a simple div centering function which is working even following the form preventDefault.
Edit: I found the issue, the onclick function was for an item that was loaded via ajax in the first function. so the selector was probably not finding it
Below is my code:
// Submit zip code
$("#get_zip").submit(function (zip)
{
//Send zipcode
$.ajax({
type: "POST",
url: "ajax_calls.php",
data: "zip="+$('#zip').val()+"&send_zip=true",
success: function(listing){$("#wrapper").html(listing); $("#lightbox,#welcome").fadeOut(300);},
error: function(){alert(3);$("#error").text("Could not retrieve posts").fadeIn(300).delay(900).fadeOut(300)}
});
//Stop form submission action
zip.preventDefault();
});
//Center item box
$("#item").centerInClient();
//When clicking an item row
$("tr.item").click(function()
{
// Get item id from selected row id
var id = $(this).attr('id');
//Fill item box with item details
$.ajax({
type: "POST",
url: "ajax_calls.php",
data: "id="+id+"&get_item=true",
success: function(r){$("#item_detail").html(r); $("#lightbox, #item").fadeIn(300);},
error: function(){alert(2);$("#error").text("Could not retrieve posts").fadeIn(300).delay(900).fadeOut(300)}
});
});
Any Help on this would be very much appreciated.
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将:更改
为:
我认为
preventDefault()
正在冒泡并取消您的点击处理程序。Try changing:
to:
I think that
preventDefault()
is bubbling up and canceling your click handler.