JQuery submit () won't work for me undefined property?

Here 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 will not present 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 in order, I only need to submit the form, why does this not work?

Update:

The console says:

 Uncaught TypeError: Property 'submit' of object #<HTMLFormElement> is not a function jquery.min.js:3 f.event.trigger 
+4
source share
5 answers

You have made the following errors:

  • The jQuery selector 'form' is too general and returns an array. So change it to '#myform' .
  • Also your javascript never allows you to submit your form. The criminal is the last line: return false; . Even after successfully validating the form, this line blocked it. This should be return true; . This error was indicated in [11684], but no one understood it and was not slowed down by some.

In any case, the code is debugged here.

  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; } 
+4
source

To raise an event, you call the corresponding function without such a parameter:

 $('form').submit(); 

Your current code simply adds a submit handler function that will be executed when the form is submitted; it does not actually submit the form.

+3
source

Try:

 $('#myform')[0].submit(); 
+3
source

As Rory said, only an event handler will add your code. You need to do

 $(form).submit() 

without parameters. If you want to attach a handler and then fire the event later, you can use

 $(form).trigger('submit') 
0
source

I had a similar problem, except that I was trying to submit the form from Javascript, rather than attaching an onsubmit event handler. The problem was that the submit button on the form I was working with had name = "submit" ... I suspect that this was part of what caused the problem with the original poster. When a form has a button called submit, it creates a submit property on the form object, overwriting the usual value of the submit property, which is a function that represents the form.

See javascript submit () is not a function? for more details

0
source

Source: https://habr.com/ru/post/1414504/


All Articles