使用Ajax表单提交?

时间:2016-04-25 20:27:01

标签: ajax forms

我的表单提交,它不应该因为我使用的是aJax?!如果我删除表单标签并关闭表单标签,那么它可以工作,但不会上传图像

$('#savebutton').click(function() {
preventDefault();
var val1 = $('#rsPubName').val();
var val2 = $('#address').val();
var val3 = $('#rsLat').val();
var val4 = $('#rsLong').val();
var val5 = $('#add2').val();
var val6 = $('#rsTown').val();
var val7 = $('#rsCounty').val();
var val8 = $('#rsPostCode').val();
var val9 = $('#region').val();
var val10 = $('#PremisesType').val();
var val11 = $('#rsTel').val();
var val12 = $('#rsWebsite').val();
var val13 = $('#rsAboutpub').val();
var val14 = $('#img1').val();
var val15 = $('#offer1').val();
$.ajax({
    type: 'POST',
    url: 'addpub-script.php',
    enctype: 'multipart/form-data',
    data: { rsPubName: val1, address: val2, rsLat: val3, rsLong: val4, add2: val5, rsTown: val6, rsCounty: val7, rsPostCode: val8, region: val9, PremisesType: val10, rsTel: val11, rsWebsite: val12, rsAboutpub: val13, img1: val14, offer1: val15},
    success: function(response) {
        $('#result').html( '<div class="alert alert-success alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button><strong>New venue added!</strong> Why not <a href="#top" class="alert-link">add another?</a></div>').fadeIn()
    }
});
});

1 个答案:

答案 0 :(得分:-1)

preventDefault();event对象上的一种方法。它应该是:

$('#savebutton').click(function(event) {
    event.preventDefault();
    ...
});

其中event是jQuery预先填充的传入参数。请参阅this page和第四种方法。