Javascript确认错误

时间:2012-01-17 05:33:02

标签: javascript html

我想在有人试图提交此删除表单时弹出确认框。确实如此,但无论您是单击“确定”还是“取消”,它都会提交表单并删除数据。这是html:

<input type='submit' onclick='delete_note_alert()' name='delete' value='Delete'></form>

这是javascript

function delete_note_alert()
{
    return confirm("Are you sure you want to delete this note?");
}

5 个答案:

答案 0 :(得分:4)

您需要在return函数和delete_note_alert属性中添加onclick语句。返回false后,偶数将被取消。

<input type='submit' onclick='return delete_note_alert()' name='delete' value='Delete'>

function delete_note_alert()
{
    return confirm("Are you sure you want to delete this note?");
}

答案 1 :(得分:0)

您可以在表单中使用onsubmit()事件

<form name="my_name" action="my_action.php" onsubmit="delete_note_alert()">
   ....  
   ....
   <input type='submit' name='delete' value='Delete'>
</form>

你可以改变你的功能:

function delete_note_alert()
{
    var c = confirm("Are you sure you want to delete this note?");
    /* Do nothing with the variable. Proceed with the code to delete, irrespective of value of variable c is true or false */
    /* Your delete code */
}
}

答案 2 :(得分:0)

你刚刚错过了一个条件。

function delete_note_alert(e)
{
    /*If the user doesn't want to delete the post*/
    if(!confirm("Are you sure you want to delete this note?"))
       /*Stop default action (form submit)*/
        e.preventDefault();
}

答案 3 :(得分:0)

你应该这样做。

function delecte_alert() { 
    var result = confirm("are you sure?");
    if (result) {
         // do something when OK clicked
    }
    else {
        // do something when cancel clicked
    }
}

答案 4 :(得分:0)

你也可以

<form method="post" enctype="multipart/form-data" onsubmit='return delete_note_alert()'>

<input type='submit'  name='delete' value='Delete'>

function delete_note_alert()
{
    return confirm("Are you sure you want to delete this note?");
}

希望这有帮助