사용자가 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를 가진 두 개의 요소를 가질 수 없습니다. ID는 페이지에서 고유해야합니다. –