2011-08-25 30 views
0

我有以下輸入字段:的jQuery - 添加例外的onclick

<input type="text" value="5" maxlength="12" id="qty" class="input-text qty" name="qty2050" tabindex="1"> 

<input type="text" value="0" maxlength="12" id="qty" class="input-text qty" name="qty2042" tabindex="1"> 

我用這個代碼,以防止用戶丟失插入的數據(在退出頁面):

<script> 
window.onbeforeunload = function() { 
    var showAlert = false; 
    jQuery('.input-text.qty').each(function(){ 

     //console.log("fonction 1"); 
     var val = parseInt(jQuery(this).val(),10); 
     if(val > 0) { showAlert = true; } 
     //alert(val); 
    }); 

    //console.log("fonction 2"); 
    //console.log(showAlert); 

    if (showAlert == true) { 
     console.log("fonction 3"); 
     return 'You have unsaved changes!'; 
    } 
} 

</script> 

我想添加一個異常到我的提交按鈕,並且當數量大於0的時候不顯示警告消息我的輸入字段。

我的問題是添加此異常!

感謝您的幫助。

+0

也許提供一些你想要的幫助信息? – Jivings

+0

我編輯了我的問題,我希望現在更清楚... – Bizboss

+0

什麼是數量> 0,你說什麼警報,也許嘗試清理你的問題... – Nayish

回答

0

您可以使用布爾confirmUnload。喜歡這裏:http://jonstjohn.com/node/23

<script> 
var confirmUnload = true; 

$("submit").click(function(){ confirmUnload = false; }); 

window.onbeforeunload = function() { 
    if (confirmUnload) 
    { 
     var showAlert = false; 
     jQuery('.input-text.qty').each(function(){ 

      //console.log("fonction 1"); 
      var val = parseInt(jQuery(this).val(),10); 
      if(val > 0) { showAlert = true; } 
      //alert(val); 
     }); 

     //console.log("fonction 2"); 
     //console.log(showAlert); 

     if (showAlert == true) { 
      console.log("fonction 3"); 
      return 'You have unsaved changes!'; 
     } 
    } 
} 

</script> 
+0

非常感謝,但似乎這部分o代碼 ' $(「submit」)。click(function(){confirmUnload = false;});' 未運行 我還添加了'confirmUnload = false;'直接指向我的按鈕(onclick),但它也不起作用:( – Bizboss

+1

沒關係,我已將它添加到我的表單中(onsubmit)現在。 非常感謝或幫助:) – Bizboss

0

這是你想要的東西:

<SCRIPT> 
function checkForm(e) { 
    if (!(window.confirm("Do you want to submit the form?"))) 
    e.returnValue = false; 
} 
</SCRIPT> 
</HEAD> 
<BODY> 
<FORM name="theForm" action="0801.html" 
    onSubmit = "return checkForm(event)"> 
<INPUT type=submit> 
</FORM> 

支票將進入checkForm方法在這種情況下

+0

是的,但是我怎樣才能調整我的代碼來完成同樣的功能呢?我仍然在jQuery開發的開始...謝謝 – Bizboss