【发布时间】:2017-05-07 00:32:30
【问题描述】:
我使用 ajax 在模态中调用表单,并使用模态按钮 .save-modal 使用 ajax 提交表单。表单提交了很多,我不知道为什么?
模态请求的页面-form-中的以下代码:
```
@section('content')
<h1>kk</h1>
<div id="modal">
{!! Form::model('App\Solution',['url' => $actionPath, 'id' => 'sForm', 'class' => 'form-inline']) !!}
<div class="form-group">
{!! Form::label('title', __('Title')) !!}
{!! Form::text('title',$solution->title,['class' =>'form-control']) !!}
@php ($eleE = $errors->first('title'))
{{-- @include('layouts.form-ele-error') --}}
</div>
<div class="form-group">
{!! Form::label('description', __('Description')) !!}
{!! Form::textarea('description',$solution->description,['class' =>'form-control']) !!}
@php ($eleE = $errors->first('description'))
{{-- @include('layouts.form-ele-error') --}}
</div>
{!! Form::close() !!}
<script>
$(document).ready(function(){
$(".save-modal").click(function(e){
alert('many time alert') //
e.preventDefault();
$.ajax({
url: '{{$actionPath}}'+'/?'+Math.random(),
type: "POST",
data: $("#sForm").serialize(),
success: function(data){
$("#modal-body").html($(data).find('#flash-msg'))
$("#actions-modal").modal('hide')
//return true;
},
error: function(xhr, status, response){
if ( status == "error" ) {
var msg = "Sorry but there was an error: ";
// $( "#modal-body" ).html( msg + xhr.status + " " + xhr.statusText );
errors = xhr.responseJSON
console.log(errors)
$("#errors").html('');
$.each(errors,function(key, val){
console.log(key)
$("#errors").append('<span class="has-error help-block">'+val+'</sapn>')
//return false;
})
xhr.responseJSON = null;
}
return false;
}
})
return false;
})
});
</script>
</div>
@endsection
$(".save-modal").click(function(e){... 之后的警报被多次警报,特别是在关闭模式并再次打开它并重复尝试保存无效条目时,警报的增加不是固定的,即它是之前尝试的无效数据提交的总和打开模态。
以下是基础页面的模态代码:
$(".action-create").click(function(e){
e.preventDefault();
alert($(this).attr('href'))
mhd = $(this).attr('title');//$(this).text()+' {{__("for Cavity")}}'+' '+$(this).attr('title');
href = $(this).attr('href')
//console.log(href)
$("#actions-modal").on('show.bs.modal', function(){
$("#modal-hd").html('<h4 style="display: inline">'+mhd+'</h4>');
$("#modal-body").html('<h4>{{__('Loading')}}<img src="/imgs/loading.gif" /></h4>')
gg(href)
})
$("#actions-modal").modal({
backdrop: 'static',
keyboard: false
});
});
$("#actions-modal").on('hidden.bs.modal', function(){
$("#modal-body").html('');
$(this).data('bs.modal', null);
//$(this).children('#errors').html('');
$("#errors").html('');
return false;
});
gg = function gg(){
$.ajax({
type: "GET",
url: href,
dataType: 'html',
success: function(data){
//console.log(data)
required = $(data).find("#modal");
$("#modal-body").html(required);
},
error: function(xhr, status, response ){
if ( status == "error" ) {
var msg = "Sorry but there was an error: ";
$( "#modal-body" ).html( msg + xhr.status + " " + xhr.statusText+ " With custom message:<br> "+ xhr.responseText );
//console.log(xhr)
}
}
});
return false;
}
我尝试在代码的许多部分添加return false 以减少任何额外的评估,我还尝试将随机数添加到ajax URL Math.random() 但它似乎执行了很多次。
在同一个页面上还有另外一个表单调用是使用modal调用的,有时除了被调用的表单之外还会被保存!
【问题讨论】:
-
@Roamer-1888 否,如果您填写了无效数据,则应在服务器端生成错误
422,响应包含字段错误消息。但是,alert和控制台日志显示,单击Save Changes即.save-modal在关闭模式后会被调用多次,然后再次重新打开它并重新尝试保存无效数据。 -
因为用户可能忘记提供经过验证的数据。 i,e 将标题字段留空。同样,这似乎不是与错误事件相关的问题,错误显示它只是因为模态保持打开状态
标签: javascript jquery ajax twitter-bootstrap