return false is not working

前端 未结 2 558
旧时难觅i
旧时难觅i 2021-01-27 09:25

I have created one form with dynamically created check box\'s . And i have used one j query script that will check weather user has checked at-least one check box or not .Tf it

2条回答
  •  生来不讨喜
    2021-01-27 09:32

    If you are going to return false from an event handler function, then you need to do it from the event hander itself, and not just another function that it calls.

    Your onclick function doesn't have a return statement. It calls Validate and then does nothing with the value that function returns.

    onclick="return Validate();"
    

    Convention reserves variable names starting with capital letters for constructor functions. Call your function validate, not Validate.

    It is generally a better idea to handle this kind of test when the form is submitted rather than when a particular submit button is clicked.

    Modern JavaScript doesn't use onclick/onsubmit/etc attributes. We use addEventListener instead.

    function validate(event) {
        // Your logic
        if (someCondition) {
            // Stop
            event.preventDefault();
        }
    }
    
    document.querySelector("form").addEventListener('submit', validate);
    

提交回复
热议问题