我正在尝试创建一个内联编辑器,它将支持点击编辑和enter/blur保存。该环境包括PHP v5.6、jQuery v3.1、Laravel v5.2和MySQL。
问题是,虽然使用enter保存有效,但模糊保存无效。
Laravel DB:输入时收听保存:
2016-08-10 12:01:45: select * from `user` where `user`.`id` = '15' limit 1
2016-08-10 12:01:45: update `user` set `name` = '22222', `updated_at` = '2016-08-10 12:01:45' where `id` = '15'
Laravel DB:听一下关于模糊的保存,注意根本没有“更新”查询:
2016-08-10 11:21:53: select * from `user` where `user`.`id` = '15' limit 1
<input>
文本已更改。我应该如何解决?
inline-edit.js
:
var edit = $(".inline-edit");
edit.click(function() {
$('.ajax').html($('.ajax input').val());
$('.ajax').removeClass('ajax');
$(this).addClass('ajax');
$OLDVAL = $.trim($(this).text());
$(this).html('<input id="inline-editbox" type="text" value="' + $OLDVAL + '">');
// focus and move cursor to the end of the editbox
var inline_editbox = $('#inline-editbox');
inline_editbox.focus();
var editbox_value = inline_editbox.val();
inline_editbox.val('');
inline_editbox.val(editbox_value);
//
inline_editbox.blur(function(event) {
var formData = {
tag: $(this).attr('id'),
value: $('.ajax input').val(),
};
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
},
type: "PATCH",
data: formData,
dataType: 'json',
success: function(data) {
$('.ajax').html($('.ajax input').val());
$('.ajax').removeClass('ajax');
},
error: function(xhr, status, error) {
console.warn(xhr.responseText);
alert(error);
}
});
});
});
edit.keydown(function(event) {
if (event.keyCode == 13) {
var formData = {
tag: $(this).attr('id'),
value: $('.ajax input').val(),
};
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
},
type: "PATCH",
data: formData,
dataType: 'json',
success: function(data) {
$('.ajax').html($('.ajax input').val());
$('.ajax').removeClass('ajax');
},
error: function(xhr, status, error) {
console.warn(xhr.responseText);
alert(error);
}
});
}
});
Laravel route.php的一部分:
Route::patch ('user/{id}', ['as'=>'user.patch', 'uses'=>'UserController@update_ajax']);
Laravel UserController.php的一部分:
function update_ajax($id, UserCreateFormRequest $request)
{
$user = User::findOrFail($id);
$tag = $request->get('tag');
if( $tag == 'name') {
$user->update([
'name' => $request->get('value')
]);
}
if( $tag == 'email') {
$user->update([
'email' => $request->get('value')
]);
}
if( $tag == 'isRegistered') {
$user->update([
'isRegistered'=> $request->get('value')
]);
}
return response()->json([
'status' => 'success',
'msg' => 'Data created successfully',
]);
}