我有一个表单和一个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()数据。
答案 0 :(得分:0)
我最终使用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;
});