[英]JavaScript confirm cancel does not work on Laravel Form
I have this confirm: 我确认:
<script type="text/javascript">
function ConfirmDelete() {
if (confirm("Are you sure you want to delete?")) {
return true;
}
else {
return false;
}
}
</script>
and then I have my Laravel form: 然后我有我的Laravel表格:
{!! Form::open(['action' => ['Test\\TestController@destroy', $thread->id], 'method' => 'delete', 'onsubmit' => 'ConfirmDelete()']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
I'm getting the message and if I'm pressing on OK, the thread gets deleted, but that also happens on cancel. 我收到消息,如果按“确定”,该线程将被删除,但在取消时也会发生。 What can I do so that nothing happens, if I'm pressing cancel? 如果我按“取消”,我该怎么办以至于什么也没发生?
You can further tidy up your javascript 您可以进一步整理JavaScript
<script type="text/javascript">
function ConfirmDelete() {
return confirm("Are you sure you want to delete?");
}
</script>
Also, you need to make sure you return the function's response onsubmit, for example 另外,您需要确保在提交时返回函数的响应,例如
{!! Form::open(['action' => ['Test\\TestController@destroy', $thread->id], 'method' => 'delete', 'onsubmit' => 'return ConfirmDelete()']) !!}
更改为此:
{!! Form::open(['action' => ['Test\\TestController@destroy', $thread->id], 'method' => 'delete', 'onsubmit' => 'return ConfirmDelete()']) !!}
You need to add return statement before function call . 您需要在函数调用之前添加return语句 。
Change the following code 更改以下代码
'onsubmit' => 'ConfirmDelete()'
to 至
'onsubmit' => 'return ConfirmDelete()'
You could also inline it, if you want ;) 如果需要,也可以内联它;)
'onsubmit' => 'return confirm("Are you sure you want to delete?")'
When calling a JavaScript function in an onclick
event handler, the return value of an event handler determines whether or not the default browser behavior should take place as well . 在onclick
事件处理程序中调用JavaScript函数时,事件处理程序的返回值确定默认浏览器行为是否也应发生 。
That's the reason why it is getting deleted even when you press Cancel
from your confirm dialog. 这就是为什么即使您在确认对话框中按“ Cancel
也将其删除的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.