我真的在几个小时内为代码的和平而苦苦挣扎:
$('#property_image_uploader').on('submit',function(e) {
var formData = new FormData(this);
$.ajax({
type:'POST',
url: $(this).attr('action'),
data:formData,
cache:false,
contentType: false,
processData: false,
success:function(data){
$.ajax({
contentType: 'application/x-www-form-urlencoded;charset=UTF-8'
, method: 'post'
, url: 'property_info_box.php'
, data: {id: parseInt(data)}
, success: function(page){
$('#property_info_box', parent.document).html(page);
return false; }
});
},
error: function(data){ console.log(data); }
});
});
$("#property_image_chooser").on("change", function(e){
$("#property_image_uploader").submit();
});
问题是每次$('#property_image_chooser')更改时,$('#property_image_uploader')子项都会被触发两次。我已经尝试将e.preventDefault()和e.stopImmediatePropagation()放在这两个函数中的任何地方,这个问题就消失了,但是,第二个ajax调用上加载的页面冻结了,没有按钮可以被触发了...
答案 0 :(得分:0)
感谢所有问题都通过在submition上添加e.preventDefault()并删除' return false'来解决。在第二个ajax电话。最终代码如下所示:
$('#property_image_uploader').on('submit',function(e) {
var formData = new FormData(this);
$.ajax({
type:'POST',
url: $(this).attr('action'),
data:formData,
cache:false,
contentType: false,
processData: false,
success:function(data){
$.ajax({
contentType: 'application/x-www-form-urlencoded;charset=UTF-8'
, method: 'post'
, url: 'property_info_box.php'
, data: {id: parseInt(data)}
, success: function(page){
$('#property_info_box', parent.document).html(page); }
});
},
error: function(data){ console.log(data); }
});
});
$("#property_image_chooser").on("change", function(e){
e.preventDefault();
$("#property_image_uploader").submit();
});
非常感谢你们......