3

I would like to add a delete confirmation to my form button Possible javascript to use??

<script type="text/javascript"> 
function confirmDelete() { 
return confirm("Are you sure you want to delete?");   
} 
</script> 

This is currently working inside my form.

<input type="button" onclick="parent.location='<?php echo "delete.php?id=" . $people['id'] ?>'" value='Delete'>

How can I integrate the javascript into the onclick event? Either using the above javascript or any other way that may be better

4

7 に答える 7

8

それが可能だ :)

タグで、属性 onsubmit を次のように使用します。

<form onsubmit="return confirm('Are you sure?');">

またはメソッドを使用して

<form onsubmit="return myMethod">

onclick イベントのボタンについても同じです。false を返すと、クリックされません。

メソッドが false を返す場合、フォームは送信されません。

于 2012-07-24T20:19:18.740 に答える
2

まず、次のように、HTML に onclick メソッドを追加します。

**HTML:**
<a class="btn btn-danger" href="some url" onclick="DeleteItem();"></a>
**JavaScript:**
function DeleteItem() 
{
     return confirm("Are you sure to delete this")
}
// Or you can simply do it like below:
<button onclick="javascript: return confirm('Are you sure to delete?');">Delete</button>
于 2016-12-21T07:10:54.427 に答える
2

javascript を onclick イベントに統合するにはどうすればよいですか?

<input type="button" onclick="javascript:confirmDelete();" value='Delete'>

<script type="text/javascript"> 
function confirmDelete() { 
var status = confirm("Are you sure you want to delete?");   
if(status)
{
 parent.location.replace("parent.location='<?php echo "delete.php?id=" . $people['id'] ?>'");
}
} 
</script> 
于 2012-07-24T20:28:12.417 に答える
1

イベント ハンドラーを JavaScript でバインドします。

<input type="button" id="delete" value='Delete' />

document.getElementById('delete').onclick = function () {
    if (confirm('Are you sure?')) {
        parent.location='<?php echo "delete.php?id=" . $people['id'] ?>';
    }
};
于 2012-07-24T20:34:52.743 に答える
-1

JavaScript を使用しない場合、HTML はフォームのリセット ボタンもサポートします。次のような構造を持つことができます。

<form>
...
items in your form
...
<input type="reset">
...
</form>

リセット ボタンをクリックすると、すべてのフォーム入力がデフォルト値にリセットされます。

于 2012-07-24T20:27:20.687 に答える