简体   繁体   English

使用jQuery.click验证表单不起作用

[英]Validating form using jQuery.click does not work

Here is my code: 这是我的代码:

    $('input#price_match_submit').click(function(event) {
        if ($.trim($('input#price_match_competitor_price').val()) == '') {
            alert("Please enter competitor's price.");
            return false;
        }
        if ($.trim($('input#price_match_name').val()) == '') {
            alert("Please enter your name.");
            return false;
        }
        if ($.trim($('input#price_match_quantity').val()) == '') {
            alert("Please enter the quantity.");
            return false;
        }
        if ($.trim($('input#price_match_email').val()) == '') {
            alert("Please enter your email address.");
            return false;
        }
        if ($.trim($('input#price_match_competitor_website').val()) == '') {
            alert("Please enter competitor's website.");
            return false;
        }
        if ($.trim($('input#price_match_phone_number').val()) == '') {
            alert("Please enter your phone number.");
            return false;
        }
    });

Here #price_match_submit is a submit button. #price_match_submit是一个提交按钮。 When I click on the button, this function should execute and validate the form. 当我单击按钮时,此功能应执行并验证表单。 But it's not working as I am expecting. 但是它没有按我期望的那样工作。 The form is being submitted without any validation. 表单未经任何验证就被提交。 Where I am doing wrong? 我在哪里做错了?

You might instead want to hook into the submit event of the parent form and need to prevent the default behaviour: 您可能想要挂接到父表单的Submit事件,并需要防止默认行为:

$('#form').on('submit', function (e) {
    if (everything_failed) {
        e.preventDefault();
        return false;
    }
});

return false; 返回false; only stops event bubbling I think. 我认为只能停止事件冒泡。

You can validate like this 您可以像这样验证

$('form').on('submit', function() {
// do validation here
       if ($.trim($('input#price_match_competitor_price').val()) == '') {
        alert("Please enter competitor's price.");
        return false;
    }
    if ($.trim($('input#price_match_name').val()) == '') {
        alert("Please enter your name.");
        return false;
    }
    if ($.trim($('input#price_match_quantity').val()) == '') {
        alert("Please enter the quantity.");
        return false;
    }
    if ($.trim($('input#price_match_email').val()) == '') {
        alert("Please enter your email address.");
        return false;
    }
    if ($.trim($('input#price_match_competitor_website').val()) == '') {
        alert("Please enter competitor's website.");
        return false;
    }
    if ($.trim($('input#price_match_phone_number').val()) == '') {
        alert("Please enter your phone number.");
        return false;
    }
});

return false if not validated. 如果未经验证,则返回false。

$(#price_match_submit').click(function(event) {
   if ($.trim($('input#price_match_competitor_price').val()) == '') {
        alert("Please enter competitor's price.");
        event.preventDefault();
    }
    else if ($.trim($('input#price_match_name').val()) == '') {
        alert("Please enter your name.");
        event.preventDefault();
    }
    else if ($.trim($('input#price_match_quantity').val()) == '') {
        alert("Please enter the quantity.");
        event.preventDefault();
    }
    else if ($.trim($('input#price_match_email').val()) == '') {
       alert("Please enter your email address.");
       event.preventDefault();
    }
    else if ($.trim($('input#price_match_competitor_website').val()) == '') {
        alert("Please enter competitor's website.");
        event.preventDefault();
    }
    else if ($.trim($('input#price_match_phone_number').val()) == '') {
        alert("Please enter your phone number.");
        event.preventDefault();
    }

    // if nothing happened until now, the form will be submited
});

Thank you for all your answer and comment. 感谢您的所有回答和评论。

This code works fine. 此代码可以正常工作。 There was some issue with other parts of code. 代码的其他部分存在一些问题。

When we assign a function to an element, the function is assigned to that physical element only. 当我们将功能分配给元素时,该功能仅分配给该物理元素。 But when we do some physical modification to the elements, all its previous properties get lost. 但是,当我们对元素进行一些物理修改时,其所有先前的属性都会丢失。 In my code, I was displaying this html in a modal popup. 在我的代码中,我正在模式弹出窗口中显示此html。 Which was copying the html instead of using the same elements. 这是复制html而不是使用相同的元素。 So, it lost this binding with the function. 因此,它失去了与功能的绑定。 And this is the reason this code was not working. 这就是此代码无法正常工作的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM