【问题标题】:Callback function of ajaxSubmit doesn't fireajaxSubmit 的回调函数不会触发
【发布时间】:2011-05-23 08:28:58
【问题描述】:

来自File upload with jQuery and CodeIgniter (But no page refresh)

@cbrandolino 建议我使用 jQuery 表单插件来解决我的问题。它运行良好,除了一个问题,回调函数没有像我预期的那样触发。

这是我修改后的代码
++ Javascript ++

$('#send_button').live('click', function(){
    var options = {
        url: 'formHandle/add',
        success: function(){
            alert('something');
        }
        //I tried both function() or function(data) 
        //or a function created outside $(document).ready()
        //None of them works
    };
    $('#new_entry_form').ajaxSubmit(options);
})

++ 控制器 ++

function add(){
    $array = array(
        'title' => $this->input->post('title'),
        'description' => $this->input->post('description'),
        'content' => $this->input->post('content'),
        'start' => $this->input->post('start'),
        'due' => $this->input->post('due'),
        'price' => $this->input->post('price'),
        'promotion_price' => $this->input->post('promotion_price'),
        'website' => $this->input->post('website'),
        'author' => $this->input->post('author'),
    );

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['overwrite'] = false;
    $config['remove_spaces'] = true;
    $config['encrypt_name'] = true;

    $this->load->library('upload', $config);
    if($this->upload->do_upload('picture')){
        $array['picture'] = $this->upload->file_name;
        $this->load->model('entry_model');
        $this->entry_model->insertEntry($array);
        echo 'true'; 
        //I tried $this->load->view, echo or return
        //none of them works
    }else{
        echo 'false';
    }
}

文件上传正常,文件已上传到特定文件夹。
数据插入工作正常,数据按预期插入数据库。
我唯一的问题是回调函数。

你知道如何解决这个问题吗?我需要显示数据插入的结果。

更新:
添加 return false 对我没有任何帮助。还是不行

【问题讨论】:

  • 可能是您的帖子(如果是帖子)标签在您提交时重新加载页面,在这种情况下返回false;在您的 ajax 调用将解决您的问题之后。
  • 嘿,我也遇到了同样的问题,你是怎么解决的?

标签: jquery codeigniter jquery-forms-plugin


【解决方案1】:

在option中写回调函数,写清楚here

$(document).ready(function() { 
var options = { 
    target:        '#output2',   

    success:       showResponse  // post-submit callback 
 }; 

// bind to the form's submit event 
$('#myForm2').submit(function() { 
    // inside event callbacks 'this' is the DOM element so we first 
    // wrap it in a jQuery object and then invoke ajaxSubmit 
    $(this).ajaxSubmit(options); 

    // !!! Important !!! 
    // always return false to prevent standard browser submit and page navigation 
    return false; 
}); 
}); 

function showResponse(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 

    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 

    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 

    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        '\n\nThe output div should have already been updated with the responseText.'); 
}

【讨论】:

  • 感谢您的回复。但是在编写代码时,我从不提交表单。我通常将通过 serialize() 方法检索到的表单数据发送到控制器。这是我第一次提交表单,但在 ajaxSubmit 的帮助下。因此,我的表单或提交按钮没有任何操作。我使用 div 作为按钮。所以,我不需要“return false;”。我认为我的代码可以正常工作,因为插入了数据,上传了图片。我只是不知道为什么回调没有按预期工作。我不知道有什么我不知道的。
【解决方案2】:

您的表单有 onsubmit="return false;" ??

【讨论】:

  • 我从不提交表单。我通常将通过 serialize() 方法检索到的表单数据发送到控制器。这是我第一次提交表单,但在 ajaxSubmit 的帮助下。因此,我的表单或提交按钮没有任何操作。我使用 div 作为按钮并向该 div 按钮添加代码(在本例中,该 div 是“send_button”)。所以,我不需要“return false;”因为根本不会提交任何表单。
猜你喜欢
  • 2011-11-23
  • 2015-03-29
  • 2012-06-20
  • 2022-11-29
  • 1970-01-01
  • 2014-01-26
  • 2010-10-09
  • 1970-01-01
  • 2015-10-27
相关资源
最近更新 更多