【问题标题】:ajaxform() & sortable('serialize'), and codeigniter .. submitting form fields and list elementsajaxform() & sortable('serialize'), 和 codeigniter .. 提交表单字段和列表元素
【发布时间】:2011-12-20 04:04:38
【问题描述】:

我有一个表单和一个 sortable() 列表,用户可以在其中从预先填充的列表中拖动到一个空的 UL 中。用户将他们想要的 LI 拖到这个空的 UL 中。还有一个带有几个文本框的表单,用户填写并单击提交。

我可以让 ajaxform 提交表单数据并在 flashdata 会话中显示它,但我不能让它显示 allowed_fields 数据。 (这是可排序的列表)。我知道它会序列化它,因为运行 alert(serializedList); 会返回序列化的元素列表。

这是生成可排序列表的 JS:

/**
 * sortable ul items
 * 
 * this is used for the add levels page to associate allowed_fields 
 * to a level.
 */
$('.block-list').sortable({
    connectWith: '.block-list',
    placeholder: 'placeholder'
});

这是处理ajaxSubmit的JS:

/**
 * showResponse(data)
 * show the response if the form submission is successful
 * @param  {object} data object of message or success
 * @return {null}
 */
function showResponse(data){
    alert(serializedList);
    if (data.errorStatus == 1){
        $.jGrowl(data.message, { theme: 'error' });
    }else{

        $.jGrowl(data.message, { theme: 'success' });
    }
}//end showResponse()

/** @type {Object} setup the options for the ajax submit forms. */
var submitOptions = {
    success: showResponse,
    beforeSubmit: function(){ serializedList = $("#allowed-fields-list").sortable('serialize');  },
    dataType: 'json',
    resetForm: true ,
    data: { allowed_fields: serializedList }
};      
$("#addlevel-form").ajaxForm(submitOptions);

这是处理表单数据的代码点火器函数..

public function addlevelprocess(){
    $message = array(
        'message' => 'Successfully Added The Level To The Database! WHOA!:'.$this->input->post(),
        'errorStatus' => 0
    );
    $this->session->set_flashdata('post', $this->input->post());
    echo json_encode($message);
}

如何让 ajaxform 发送表单字段数据和 sortables() 数据。

【问题讨论】:

    标签: codeigniter jquery-ui-sortable ajaxform


    【解决方案1】:

    我最终使用了 ajaxSubmit() 并通过数据传递了序列化的表单信息:

    $('#addlevel-form').submit(function() {
        /** @type {serialized} serialized array of field IDs for allowed fields */
        var allowedFields = $('#allowed-fields-list').sortable('serialize');
        var formInfo = $('#addlevel-form').formSerialize();
        /** @type {Object} setup the options for the ajax submit forms. */
        var submitOptions = {
            url: '/admin/levels/addlevelprocess.html',
            dataType: 'json',
            success: showResponse,
            data: { allowed_fields: allowedFields, levelInfo: formInfo }
        };      
        $(this).ajaxSubmit(submitOptions);
        return false;
    });
    

    【讨论】:

      猜你喜欢
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多