简体   繁体   中英

why my form is not being submitted even after the passwords match.?

This is my Piece of script with form id="details"

<script type="text/javascript">
function onLoad() {
    $("#password").keyup(passCheck);
    $("#repeatpass").keyup(passCheck);
}
function passCheck() {
    var password = $("#password").val();
    var repeatpass = $("#repeatpass").val();
    if (password == repeatpass) {
        $("#matchpass").text("Passwords match");
        $("#matchpass").addClass("valid");
        $("#matchpass").removeClass("error");
        $("#details").submit(true);

    } else {
        $("#matchpass").text("Passwords donot match");
        $("#matchpass").addClass("error");
        $("#matchpass").removeClass("valid");
        $("#details").submit(false);

    }
}
$(document).ready(onLoad);
</script>

I want to know why my form(id="details") is not being submitted and if someone can explain how this structure works, it will be more helpful for me. thanks

Working fiddle .

Use $("#details").submit() instead of $("#details").submit(true); and remove $("#details").submit(false); .

Hope this helps.

For starters you do not need to have a .submit call in the else block.

This code works in three steps:

(1) The .ready command tells jQuery that when the page is ready it should execute the function named onLoad .

(2) The onLoad function tells that when a key from the keyboard is pressed then the passCheck function should be called.

(3) The passCheck function checks to see if the two texts are equal and if so submits the form, otherwise it does not submit (ie The .submit should not be called at all).

Probably the two passwords are both empty initially so they are equal and the .submit is attempted but an error on that prevents the function a ms the script stops working. After that the script stops working completely.

Both notes on the request for the HTML code and the note on the argument given to the .submit , mentioned above are correct.

You should not pass parameter in your submit function remove your $("#details").submit(true); and $("#details").submit(false); its not there in jquery API write this fragment of code it will be of better use:

$( "form" ).submit(function(event) {
  if ( $( "span" ).hasClass('error')) {
   alert('prevented');
   event.preventDefault();
 }
 return;
});

Fiddle Link

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