我有下面的两个脚本,它们几乎可以做同样的事情,但它们适用于两组不同的表单,但通过ajax发布到同一页面。我的问题是,有没有一种方法可以最大限度地减少完成工作所需的代码量
例如$(document).on('submit', 'form.frm_details','form.wa_cu_del', function(event)
<script>
$(function() {
$(document).on('submit', 'form.frm_details', function(event) {
event.preventDefault();
$.ajax({
url: '/limitless/functions2.php',
type: 'post',
dataType: 'json',
data: $(this).serialize(),
success: function(data) {
if(data.status == '1') {
$('#info').html('<div class='"alert alert-success alert-styled-left alert-arrow-left alert-bordered'"> ' + data.message + '<button type='"button'" class='"close'" data-dismiss='"alert'" >×</button></div>');
$('.my-modals').modal('hide');
webapp_get_customers();
document.getElementById('frm_details0').reset();
}
if(data.status == '2') {
$('#info').html('<div class='"alert alert-danger alert-styled-left alert-arrow-left alert-bordered'">' + data.message + '<button type='"button'" class='"close'" data-dismiss='"alert'" >×</button></div>');
$('.my-modals').modal('hide');
}
}
});
});
});
</script>
<script>
$(function() {
$(document).on('submit', 'form.wa_cu_del', function(event) {
event.preventDefault();
$.ajax({
url: '/limitless/functions2.php',
type: 'post',
dataType: 'json',
data: $(this).serialize(),
success: function(data) {
if(data.status == '2') {
$('.my-modals').modal('hide');
$('#info').html('<div class='"alert alert-danger alert-styled-left alert-arrow-left alert-bordered'"> ' + data.message + '<button type='"button'" class='"close'" data-dismiss='"alert'" >×</button></div>');
webapp_get_customers();
}
}
});
});
});
</script>
您的代码发生了更改,而不是
$(document).on('submit', 'form.frm_details','form.wa_cu_del', function(event)
你必须写
$(document).on('submit', 'form.frm_details, form.wa_cu_del', function(event)