Jquery$(this).最接近('form');点击按钮后不工作
Jquery $(this).closest('form'); not working after button click
我有以下js代码:
$("#add_station").on('click', function () {
$(this).closest('form').submit(function () {
alert("working!");
$.ajax({
url: advoke.base_url + "/new-vendor-user/station/ajax",
method: 'post',
processData: false,
contentType: false,
cache: false,
dataType: 'json',
data: new FormData(this),
beforeSend: function () {
$('.info').hide().find('ul').empty();
$('.success_message').hide().find('ul').empty();
$('.db_error').hide().find('ul').empty();
},
success: function (data) {
if (!data.success) {
$.each(data.error, function (index, val) {
$('.info').find('ul').append('<li>' + val + '</li>');
});
$('.info').slideDown();
setTimeout(function () {
$(".info").hide();
}, 5000);
} else {
$('.success_message').slideDown();
$('#add_station').remove();
$("#station").append(data.new_station);
setTimeout(function () {
$(".success_message").hide();
}, 5000);
} //success
},
error: function () {
//db error
$('.db_error').append('<li>Something went wrong, please try again!</li>');
$('.db_error').slideDown();
//Hide error message after 5 seconds
setTimeout(function () {
$(".db_error").hide();
}, 5000);
} //error
});
});
return false;
});
当我点击id为add_station
的按钮时,它在$($this).closest('form').submit(function(){...)
之后的点击功能上发出警报,但它不起作用,正如你所看到的,我在提交功能之后发出了"有效"的警报。我在控制台上没有错误,我不知道问题出在哪里。此外,被点击的按钮在表单内。
我需要在里面使用$($this).closest('form').submit(function(){...)
,因为在ajax成功后,将生成一个新的表单,其中包含将使用此代码的addstation按钮。
您应该使用来阻止默认的提交触发器
e.preventDefault();
$(this).closest('form').submit(function (e) {
e.preventDefault();
<!--rest of the code-->
})
添加一个单独的提交处理程序
$("#add_station").on('click', function () {
$(this).closest('form').submit();
});
$("form").on("submit", function (e) {
e.preventDefault();
alert("working!");
$.ajax({
url: advoke.base_url + "/new-vendor-user/station/ajax",
method: 'post',
processData: false,
contentType: false,
cache: false,
dataType: 'json',
data: new FormData(this),
beforeSend: function () {
$('.info').hide().find('ul').empty();
$('.success_message').hide().find('ul').empty();
$('.db_error').hide().find('ul').empty();
},
success: function (data) {
if (!data.success) {
$.each(data.error, function (index, val) {
$('.info').find('ul').append('<li>' + val + '</li>');
});
$('.info').slideDown();
setTimeout(function () {
$(".info").hide();
}, 5000);
} else {
$('.success_message').slideDown();
$('#add_station').remove();
$("#station").append(data.new_station);
setTimeout(function () {
$(".success_message").hide();
}, 5000);
} //success
},
error: function () {
//db error
$('.db_error').append('<li>Something went wrong, please try again!</li>');
$('.db_error').slideDown();
//Hide error message after 5 seconds
setTimeout(function () {
$(".db_error").hide();
}, 5000);
} //error
});
});
ajax成功后,将使用addstation生成一个新表单将使用此代码的按钮
如果你生成了一个新按钮,你必须在点击被放置到dom后再次绑定它。
相关文章:
- Sencha Touch使一个简单的按钮工作
- Chrome扩展程序:浏览器操作单击按钮工作一次
- jquery如何让这个对话框的“确定”按钮工作
- 我的设备后退按钮工作不正常,它转到第一个html,但返回到第二个html
- 使“编辑”按钮工作
- 如何使单选按钮工作两个任务
- 通过输入键(不起作用)或搜索按钮(工作)查询结果
- 我有三个地方供用户登录,在同一页面上,我如何在一个功能中让所有三个按钮工作和三个表单处理
- 如果 ID 消失,提交按钮工作
- 无法让Gmail一键确认操作按钮工作
- 我无法使这些按钮工作
- 如何让我的离子侧菜单按钮工作
- 试图让按钮工作的功能,但我的警报弹出了书面脚本
- 一个按钮工作,其他不,相同的脚本
- 使用jquery ajax和PHP的浏览器“后退按钮”工作
- 在Firefox上禁用JavaScript意味着我的按钮不能工作,是否有一种方法可以让按钮工作?
- 按钮onclick按钮工作奇怪
- 在这种情况下,我如何使load more按钮工作
- 让我的后退和前进按钮工作与ajax
- 当在主干中使用推送状态重定向时,我如何保持“返回”?按钮工作