简体   繁体   中英

Jquery when click on submit button, if the form is validated

I made a form, and when clicked on submit, it takes you to another webpage but i couldn't transfer the variables(values of form) from one webpage to another webpage through Javascript.

So, i figured to hide the data, when submit button clicked.

How can i check if the form is validated, and only then initiate Jquery. With the current logic, it initiates the Jquery even if the form is not initiated.

Ultimately, animate or hide the heading and the whole form and change it to the result.

<head>
    <title>Food Web App</title>
    <link rel="stylesheet" type="text/css" href="appStyleQues.less">
    <link href='http://fonts.googleapis.com/css?family=Orbitron:500' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Roboto+Mono:300' rel='stylesheet' type='text/css'>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="N:\Desktop\Javascript\App.js" type="text/javascript"></script>
</head>

<body>
    <div id="main">
        <header>
            <h1>I need some data</h1>
        </header>
        <div id="ques">
            <ul>
                <form name="myForm" method="get">
                    <li>What cuisine?(You can leave it empty)
                        <br>
                        <input list="Cuisines" class="normal" type="text" name="Cuisine" placeholder="Like Chinese" pattern="Chinese|Italian|American">
                        <datalist id="Cuisines" autocomplete="off">
                            <option value="Chinese"></option>
                            <option value="Italian"></option>
                            <option value="American"></option>
                        </datalist>
                    </li>
                    <li>On a scale of 1 to 10, how much hungry are you?
                        <br>
                        <input class="normal" type="number" name="hunger" min="1" max="10" required>
                    </li>
                    <li>
                        <input class="special" type="checkbox" name="Personality" value="Vegetarian" checked> Vegetarian
                        <input class="special" type="checkbox" name="Personality" value="Non-Vegetarian"> Non-Vegetarian
                    </li>
                    <li>On a scale of 1 to 10, how much healthy do you want the food to be?
                        <br>
                        <input class="normal" type="number" name="Calories" min="1" max="10" required>
                    </li>
                    <li>What will be the max cost of the food, per person?
                        <br>(Quality of the food will be affected)
                        <br>
                        <input class="normal" type="number" name="quality" placeholder=" Like 400" step="50" autocomplete="off" required>
                    </li>
                    <li>How many people?
                        <br>
                        <input class="normal" type="number" name="Amount of people" required>
                    </li>
                    <li>
                        <input class="normal" type="submit" onclick="return a();">
                    </li>
                </form>
            </ul>
        </div>
    </div>
</body>

</html>

The following is a VERY basic function but will point you in the right direction:

$("form").submit(function(e){
    e.preventDefault();
    var url = "test2.html";
    var appendix = "?";
    var validated = false;
    // Validation
    $("form input").each(function() {
        // Validation stuff here.
        // A basic example:
        if ($(this).val() == "") {
            alert("Please add all fields.");
            return false;
        }
        else validated = true;
    })
    if (validated == true) {
        $("form [name]").each(function() {
            appendix += this.name + "=" + this.value + "&";
        })  
        window.location.href = url + appendix;  
    }   
})

You will need to add a value to your submit button.

you could assign ids to all input tags and then access the values using $('#idofinputfield').val() . you should also use a link or button and register an onclick event handler in javascript

$( "#idofbuttonorlink" ).click(function() {
  alert( "Handler for .click() called." );

  //validation and further action

});

.if you want to keep using the submit button you can use the submit event handler which allows you to cancel the submission when the validation fails:

$( "#idofsubmitbutton" ).submit(function( event ) {
   alert( "Handler for .submit() called." );

   //validation and further action

   event.preventDefault(); //use this function to cancel submission
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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