Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery submit() will not work for me undefined property?

Tags:

jquery

submit

This is what I got:

<form method="post" id="myform" class="myform">
    <input type="submit" onclick="return claim();" name="submit" class="onsubmit" value="" />
</form>
function claim() {
    var c = confirm('You sure?');

    if (!c) {
        return false;
    }

    var password = prompt("Please mention pw","");
    if (password != null && password != "") {
        $.post(
            "/claim/", 
            { partner_pwd: password },
            function(data) {
                if (data == '1') {
                    $('form').submit(function() {
                        alert('Handler for .submit() called.');
                    });
                }
            });
        }

        return false;
    }

This works well, but my problem is that it won't submit the form. I tried both $('form').submit() and $('#myform').submit() inside the data == '1' statement. I tried alert(1) inside this if statement, and it displayed fine, I only need to make it submit the form, why wont this work?

Update:

Console says:

Uncaught TypeError: Property 'submit' of object #<HTMLFormElement> is not a function jquery.min.js:3 f.event.trigger
like image 478
Karem Avatar asked Dec 27 '22 00:12

Karem


2 Answers

You made the following mistakes:

  1. jQuery selector 'form' is too general and returns an array. So change it to '#myform'.
  2. Also your javascript never allows your form to be submitted. The culprit is the last line : return false;. Even after successfully validating the form, this line blocked it. It should have been return true;. This mistake was pointed out by @11684, but no one understood him and was downvoted by some.

Anyways, here's the debugged code.

 function claim()
{
    var c = confirm('You sure?');

    if (!c) {
        return false;
    }

    var password=prompt("Please mention pw","");
    if (password!=null && password!="")
      {
            $.post("/claim/", { partner_pwd: password },
               function(data) {
                    if(data == '1')
                    {
                        $('#myform').bind('submit',function() {
                          alert('Handler for .submit() called.');
                        });
                    }
               });
      }

    return true;

}
like image 86
Tabrez Ahmed Avatar answered Jan 12 '23 18:01

Tabrez Ahmed


To raise an event, you call the relevent function without a parameter like this:

$('form').submit();

Your current code is simply adding a submit handler function to be executed when the form is submit, it is not actually submitting the form.

like image 22
Rory McCrossan Avatar answered Jan 12 '23 18:01

Rory McCrossan