[英]Javascript Confirm Dialog Not Activiting
這是代碼:
<form method="post" id="cp_ind_form">
// lots of input fields!
<input type="submit" name="update_submit" value="Update" />
<input type="submit" name="delete_submit" value="Delete" onclick="deleteConfirm()" />
</form>
scripts.js (確認此文件已連接到上一頁)
(function deleteConfirm() {
var s = document.getElementById('confirm');
s.onchange = function() {
var yes = confirm('Do you really want to delete this location?');
if (yes) {
var f = document.getElementById('cp_ind_form');
f.submit();
}
}
})();
}
這讓我發瘋。 這樣的基本功能在這里不起作用? 我基本上是從其他我確實可以正常工作的代碼中復制它,這沒有什么不同。 有人可以確認我是否缺少一個小細節?
看代碼。
當您單擊提交時,您正在向某個事物添加onchange事件。
您沒有觸發確認顯示,也沒有取消原始提交。
並且用(funciton(){})();包裝它沒有任何意義。
它應該是
function deleteConfirm() {
return confirm('Do you really want to delete this location?');
}
和
onclick="return deleteConfirm();"
要在調用deleteConfirm
時實際執行確認,請將代碼更改為此:
function deleteConfirm() {
var yes = confirm('Do you really want to delete this location?');
if (yes) {
document.getElementById('cp_ind_form').submit();
}
// block default submit
return(false);
}
這也將返回false,以使默認表單提交不會發生。
然后,將HTML更改為此:
onsubmit="return deleteConfirm()"
如果您處理onsubmit而不是onclick,則可以通過從處理程序返回false來阻止默認提交。
deleteConfirm()
在單擊按鈕時運行。 在此函數內部,您正在為事件onchange
分配代碼,然后等待事件#confirm
更改。 我不知道該元素在哪里,但我認為事件從未觸發過,因此確認框從未顯示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.