簡體   English   中英

onsubmit返回確認進入URL

[英]onsubmit return confirm go to url

我有一個確認彈出窗口。 僅當用戶未登錄時才顯示彈出窗口。

<form method="post" action="http://domain.com/go-here.php" <?php if (sw::shared()->session->user_type !== 'customer') { ?>onsubmit="return confirm('You must be logged in to register for this game. Press ok to go to the login page.');"<?php } ?>

當用戶在彈出窗口上單擊“確定”時,我希望將其帶到http://domain.com/log-in.php而不是原始表單操作目標。

我可以用內聯JS實現嗎?

session->user_type !== 'customer') { ?>onsubmit="var conf = confirm('You must be logged in to register for this game. Press ok to go to the login page.'); if (conf == true) { console.log('said yes');} else { console.log('said no');}"

必須將確認診斷日志分配給變量,以便可以訪問確認狀態,該狀態在“確定”時返回true,在“取消”時返回false。

[編輯:回答您的問題的下一部分

為了防止在單擊“提交”按鈕時提交表單,您將要抓住“提交”按鈕的單擊並使用事件方法e.preventDefault() 但是,由於許多原因,內聯很難做到。 如果它必須是內聯的:最好的選擇是使用<input type='submit' />以外的其他按鈕來提交表單。 因此,為您的表單提供一個ID,以便您可以在按鈕的onclick中獲取它。 本質上,您將擁有

<button onclick='var conf = confirm("blah blah"); if (conf == true) { document.getElementById("myForm").submit();} else { console.log("they hit cancel");}'>SUBMIT</button>

]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM