Ajax 添加记录,然后用更新的数组重新填充select字段
情景:拉雷维尔4.2 我有一个带有选择框的表单Ajax 添加记录,然后用更新的数组重新填充select字段,ajax,laravel,laravel-4,Ajax,Laravel,Laravel 4,情景:拉雷维尔4.2 我有一个带有选择框的表单 {{ Form::select('NameCustomary', $ent) }} 使用预先填充的人名。 在控制器中: $ent = array('' => 'Choose existing person') + Entity::orderBy('NameCustomary','asc')->lists('NameCustomary', 'id'); 我需要的是: 如果某个人(型号名称为实体)不在列表中,我希望 添加新实体,然后使
{{ Form::select('NameCustomary', $ent) }}
使用预先填充的人名。
在控制器中:
$ent = array('' => 'Choose existing person') + Entity::orderBy('NameCustomary','asc')->lists('NameCustomary', 'id');
我需要的是:
如果某个人(型号名称为实体)不在列表中,我希望
- 添加新实体,然后使用更新的$ent重新填充select李>
- 如果可能,我希望在新添加的名称上设置选择框,以避免用户遇到麻烦。其他信息:选择框正在处理select2脚本
谢谢。我想出了这个解决方案。它是粗糙的,但它的工作原理是令人满意的 首先,在阅读之后 我设法使用AJAX发布了一个表单 重要提示: 使用AJAX的表单必须放在要修改的表单之外!我使用了一种模式——通过这种方式,可以使用原始表单中的按钮触发AJAX表单 二,, 在Controller方法中,我向数据库添加了新实体,并包含了以下响应数组:
$response = array(
'status' => 'success',
'msg' => 'Option created successfully',
'new_entity_id' => '<input class="" name="NameCustomary" type="hidden" value="'.$LastInsertId.'" id="NameCustomary">',
'new_entity' => Input::get( 'NameCustomary' )
);
return Response::json( $response );
}))
注意上面JS中的最后一个命令:它是关闭模态的命令。它由将表单发送到控制器方法的相同单击触发。它发生在原始形式被替换且模态不再必要后-
完成了
如果您知道一个更好的解决方案或有办法改进上述代码,请这样做
jQuery(document).ready(function($){
$('#form-quick-entity').on('submit', function(){
// ajax post method to pass form data to the
$.post(
$(this).prop('action'), {
"_token": $( this ).find( 'input[name=_token]' ).val(),
"option_name": $( '#kv_option_name' ).val(),
"option_value": $( '#kv_option_value' ).val(),
"NameCustomary": $( this ).find( 'input[name=NameCustomary]' ).val(),
"itemtype_id": $( this ).find( 'input[name=itemtype_id]' ).val()
},
function(data){
//response after the process.
$('#experiment_update2').text(data.msg);
$('#entity_place').replaceWith(data.new_entity_id);
$('#entity_name').text(data.new_entity);
$('#entity_name').addClass('but- block padding csh_11 margins');
$('#entity_name').removeClass('hidden');
$('#modal_quick_add_entity').modal('hide');
},
'json'
);
return false;
});