【问题标题】:I have two buttons on a page, save and complete. I'm looking for two different alerts for two input fields我在一个页面上有两个按钮,保存和完成。我正在为两个输入字段寻找两个不同的警报
【发布时间】:2013-04-09 18:23:40
【问题描述】:

如果没有输入charge_amt 输入框的值,我正在使用onbeforeunload jquery 事件通知用户他们没有输入金额。输入金额后,他们可以保存。

<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>

【问题讨论】:

    标签: php javascript jquery onbeforeunload


    【解决方案1】:

    您可以检查用户是否在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;
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多