开发者

jQuery: Button cause validation [duplicate]

开发者 https://www.devze.com 2023-03-12 00:44 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: jQuery: Button cause validation
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

jQuery: Button cause validation

I have a form with a submit button. This submit button validate my form with jQuery. The problem is, in my form I have some with onclick's event which call javascript method. But when these "non-submit" button are clicked, the form is valid开发者_JAVA百科ated.

How I can use without trigger the validation of the form?


From what it sounds like you need something like this:

$('#formid').submit(function(){
    //validate here
});

this is assuming you have an input of type submit


This answer is based on insufficient information. Do you validate form on submit event? Your problem seems like you use without specifying type (default type is "submit"). In that case try:

<button type="button" id="myButton">Non validating action</button>

Otherwise you could use variable indicating whether validation is required. In your validating function just check state of that variable.


Maybe this would work for you?

$(document).ready(function(e){
    $('#clickMe').click(function(e){
        alert("click me!");
    });

    $('#clickMeAgain').click(function(e){
        alert("click me again!");
    });

    $('#submitMe').click(function(e){
        alert("submit me!");

        if(formIsValidated){
            $('form').submit();
        }
    });
});

<form>
    <input type="button" id="clickMe" value="Click Me"/>
    <input type="button" id="clickMeAgain" value="Click Me Again"/>

    <input type="button" id="submitMe" value="Submit Me"/>
</form>
0

精彩评论

暂无评论...
验证码 换一张
取 消