javascript Javascript确认删除 - 如果没有不提交表单?代码点火器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6943871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript confirm delete - if no do not submit form? Codeigniter
提问by sqlmole
Is there a piece of Javascript that would stop a form submit button from carrying out a function
是否有一段 Javascript 可以阻止表单提交按钮执行某个功能
I am using the CodeIgniter framework which uses MVC pattern.
我正在使用使用 MVC 模式的 CodeIgniter 框架。
So when you click the button it calls a controller function which then calls a model function etc.
所以当你点击按钮时,它会调用一个控制器函数,然后调用一个模型函数等。
I tried using various things in Javascript including go back, history go back. Is there anything that will stop any event happening?
我尝试在 Javascript 中使用各种东西,包括回溯、历史回溯。有什么可以阻止任何事件发生吗?
Here is my code:
这是我的代码:
function confirmation() {
var answer = confirm("Delete Record?")
if (answer)
{
}
else
{ //reloads current page - however it still continues to function
window.redirect("http://localhost/midas/index.php/control_panel/user_records");
}
}
It still continues to do the function if I say "Cancel". Is there any way to stop the browser?
如果我说“取消”,它仍然会继续执行该功能。有什么办法可以停止浏览器吗?
This is my php code:
这是我的 php 代码:
//js = is a variable which stores the javascript code
echo form_submit('view','View', $js);
This creates html output like so:
这会像这样创建 html 输出:
<input type="submit" onclick="confirmation()" value="View" name="view">
回答by jondavidjohn
You need to put the javascript event in your form tag and use onSubmit
instead of onClick
, also you don't need an external js for this.
您需要将 javascript 事件放在表单标记中并使用onSubmit
而不是onClick
,您也不需要为此使用外部 js。
<form action="url/to/form" onSubmit="return confirm('are you sure?')">
</form>
回答by Evan Larsen
On the button which is causing the POST.. then put a function call on the OnClick event for that button. Have that function return true to continue with the POST or return false to cancel the action.
在导致 POST 的按钮上。然后在该按钮的 OnClick 事件上调用函数。让该函数返回 true 以继续 POST 或返回 false 以取消操作。
回答by yokoloko
function confirmation() {
var answer = confirm("Delete Record?")
if (answer) {
} else {
//reloads current page - however it still continues to function
return false;
}
}
Maybe returning false to the method will stop the form submit. Depends how you're calling the confirmation function.
也许向该方法返回 false 将停止表单提交。取决于您如何调用确认功能。
回答by Lance
Try adding return false;
at the end of your else
statement after your window.redirect
.
尝试return false;
在else
语句末尾添加window.redirect
.
Edit:
编辑:
After looking at your onclick
event code I would suggest changing your onclick
to look something like this:
查看您的onclick
事件代码后,我建议您将其更改onclick
为如下所示:
<input type="submit" onclick="confirmation(); return false;" value="View" name="view">
回答by pit
using codeigniter form helper it would be :
使用 codeigniter 表单助手它将是:
echo form_open('url/to/form', array('onsubmit'=>'return confirm(\'use triple escape to escape special chars like \\'quotes\\'. \')'));
回答by twmulloy
I create a view helper and do something to this effect
我创建了一个视图助手并为此做一些事情
function delete_button($id)
{
$button = form_open(current_url().'/delete');
$button .= form_hidden('id', $id);
$button .= form_submit(array(
'name' => 'delete',
'onclick' => "return confirm('are you sure you wish to delete?');"
), 'Delete');
$button .= form_close();
return $button;
}
then in my view, i'll use <?= delete_button($row->id) ?>
to render the necessary form and submit (delete) button code. since the javascript confirm()
method returns a boolean, we can simply just return
the method since it'll return false
when the user cancels on the confirmation dialog and naturally a click event that returns false;
will prevent it from proceeding to the form action.
然后在我看来,我将<?= delete_button($row->id) ?>
用来呈现必要的表单并提交(删除)按钮代码。由于 javascriptconfirm()
方法返回一个布尔值,我们可以简单地只return
返回该方法,因为它会false
在用户取消确认对话框时返回,并且自然会返回一个单击事件,这returns false;
将阻止它继续进行表单操作。
回答by Hridesh Kumar
PHP code:
PHP代码:
$attributes = array('id' => 'formId','onsubmit'=>'return validate()');
echo form_open('controller', $attributes);
where validate() is the javascript function being called on submit. And if it return false, form will not submit. :-)
其中validate() 是在提交时调用的javascript 函数。如果它返回false,表单将不会提交。:-)