<strong>Javascript程式碼</strong> <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="77459" class="copybut" id="copybut77459" onclick="doCopy('code77459')"><u>複製程式碼</u></a></span> 程式碼如下:</div> <div class="codebody" id="code77459"> <br><br> 程式碼如下:<br><br> <br>頁面時判斷是否有未儲存的輸入值<br>var hasSaved = false;//是否有輸入的內容未儲存標誌,初始值為false <br>function CheckUnsave(){ <br>if(hasSaved==false ){ <br>alert("您上傳的東西尚未儲存,請儲存後再離開頁面"); <br>return false;} <br>//return true; //不能加這個語句<br>} <br><br>//保存了則改變狀態<br>function ChangeState() <br>{ </div>hasSaved = true; <br>} <br> <strong></strong> <br> <div class="codetitle"> <span><a style="CURSOR: pointer" data="69173" class="copybut" id="copybut69173" onclick="doCopy('code69173')"> <u> </u>在body中加入屬性onbeforeunload </a></span>Html代碼</div> <div class="codebody" id="code69173"> <br><br> </div> <br>複製代碼<br><br><strong> 代碼如下:</strong><br> <div class="codetitle"> <!--一定要加return--> <span><a style="CURSOR: pointer" data="47147" class="copybut" id="copybut47147" onclick="doCopy('code47147')"> <u>要在提交action之前呼叫ChangeState. </u>如果按下某個按鈕後離開頁面則不再提醒,例如儲存的按鈕,則要在儲存的按鈕按下以後,要修改下是否儲存的標誌符號的狀態eg: </a></span>Html程式碼</div> <div class="codebody" id="code47147"> <br><br> </div>複製程式碼 程式碼如下: <input type="submit" name="Submit" value="保存" onclick="ChangeState();">