ユーザーがPHPを使用して変更できるフォームを使用してページを設定しました。ページから離れて移動しようとすると、JSを使用して警告ボックスが表示されます。ただし、[保存]ボタンをクリックすると、アラートボックスは引き続き表示されます。特定のボタンをクリックしない限り、警告ボックスをユーザーに表示する方法
ユーザーが保存ボタンをクリックしたときに警告ボックスが表示されなくなることはありますか?ここ
は私のJSです:すべての
var needToConfirm = true;
$('#save').click(function(){
var needToConfirm = false;
})
if (needToConfirm == true)
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Here is my HTML (just the button): -->
<input type="submit" id="save" />
<input type="hidden" id="save" name="submitted" value="TRUE" />
同じIDを持つ2つの要素を持つことはできません。 IDはページ全体で一意である必要があります。 –