2013-04-09 34 views
0

兩個不同的警報如果我使用的分配時通知他們並沒有輸入金額用戶的onbeforeunload jQuery的事件沒有輸入charge_amt輸入框的值。他們輸入金額後可以保存。我在頁面上有兩個按鈕,保存並完成。我找了兩個輸入字段

<td><input type="hidden" name="charge_cc" value="0" class=""> 
     <input type="checkbox" id="charge_cc" name="charge_cc" value="1" checked="" class=""> Charge CC? Amount $ <input type="text" id="charge_amt" name="charge_amt" value="523" size="8" maxlength="6"></td> 

<script> 
$(document).ready(function(){ 
    $("input[type='text'], select, textarea").change(function(){ 
     window.onbeforeunload = function() 
      { 
      return "You have not saved an amount to be charged."; 
      } 
    }); 
    $("charge_amt").submit(function(){ 

     window.onbeforeunload = null; 
    }); 
}); 
</script> 

第二個事件是完整的按鈕。

<input type='checkbox' name='reorder_comment_for_CC'> Verify Charge and Leave Comment" ?> 

如果他們去打完成,但該複選框沒有被標記我想提醒他們,他們將無法未經覈實/檢查完成事件。

也許我應該只使用兩個獨立的事件,就像這樣:

<script type="text/javascript"> 
$("#charge_amt").keypress(function() { 
    if($(this).val().length > 1) { 
     return "You have not input an amount to be charged."; 
    } else { 
     return "Thank you for entering a charge amount, you can now save."; 
    } 
});</script> 

回答

0

您可以同時檢查用戶是否在charge_amt輸入了正確的值,如果他們已經檢查這樣的複選框。你似乎也對jQuery語法有點困惑。您應該使用#the_id_of_the_element來查找單個元素。該submit()事件連接到您的形式沒有到外地charge_atm

$("#id_of_your_form").submit(function() { 
    //Here you can check the checkbox 
    //This assumes you add the id of the checkbox to be the same as the name 
    var $checked = $(this).find("#reorder_comment_for_CC").is(":checked"); 
    var $validValue = $(this).find("#charge_amt").val().length > 0;   

    if (!$checked) { 
     alert("You haven't verified charge"); 
     return false;//prevent submitting 
    } 

    if (!$validValue) { 
     alert("Invalid value"); 
     return false; 
    } 

    return true; 

}); 
相關問題