0

我是一个新手程序员。我被一个简单的编码问题困扰了两天。我尝试使用jquery表单插件将表单提交到另一个页面并从该页面获取反馈。问题是插件不起作用,表单正常提交而没有反馈. 这是代码:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
<script src="http://malsup.github.com/jquery.form.js"></script>

<div id='preview'></div>
<form  action='ajaxcall.php' id='upload_pic' enctype='multipart/form-data'  method='post'>
<input type='file' id='pic' name='picture'>
<input type='submit' id='sub'>
</form>

var options=
{
  target:'#preview',
  url:'ajaxcall.php'
};

$(document).ready(function(){
    $("#sub").click(function(){
        $('#preview').html("<img src='images/loader.gif' alt='Loading.....'/>");
        $('#upload_pic').ajaxForm(options).submit();
    });
});

这是我的 ajaxcall.php 页面代码

if(!empty($_FILES['picture']['size']))
{
 echo "<img src='images/197.jpg'>";
}

期望是回显的图像会反馈,但页面只是重定向到 ajaxcall.php 页面。我知道 ajaxForm() 函数不起作用。我在 SO 中问了两次同样的问题,但仍然没有令人满意的解决方案。但为什么?请帮助。在此先感谢。

4

1 回答 1

0

Use adeneo's answer, because I also do not see why you would use this plugin, but here could be a few reasons why what you have isn't working:

In the examples from the plugin's website, you should

  1. bind to the form, not the button
  2. use the beforeSubmit option
  3. Remove the submit() from the end of your call, not sure why it is there
  4. Watch console for any errors
var options=
{
  target:'#preview',
  url:'ajaxcall.php',
  beforeSubmit:  showLoading,
  error: function(e){
      alert("Error: " + e);
  }
};

function showLoading(){
    $('#preview').html("<img src='images/loader.gif' alt='Loading.....'/>");
}

$(document).ready(function(){
    $("#upload_pic").ajaxForm(options);
});
于 2013-03-24T12:59:54.887 回答