javascript - jquery - .submit() function not working inside ajax() function -


I'm new to Ajax. The requirement is quite simple when the submit button is pressed, javascript makes an asynchronous request, which will validate different side user input. If the input is ok, then the form standard will submit, otherwise there will be an error message.

So, this is my form:

  & lt; Form id = "form" action = "dosomething.jsp" method = "post" & gt; & Lt; Input type = "text" name = "name" & gt; & Lt; Input type = "text" name = "email" & gt; & Lt; Input id = "submit" type = "submit" value = "go" & gt; & Lt; / Form & gt;   

This is my javascript:

  $ (document) .ready (function () {$ ("# form"). Submit (function (event )} {Event.preventDefault (); var form = $ (this); var button = $ ("# submit") button. Reuters ("disabled", "disabled"); $ .aex ({url: "valid Search: "post", data: form.serialize (), success: function (msg) {if (msg == "") {form.unbind ("submit"); form.submit ();} Else {alert (msg); button .removeAttr ("disabled");}}});});});   

The code works fine, except for a form.submit (); is not working I will try it out of the AJAX () function, it will be deposited, so I think there is a problem about the scope, but I can not fix it.

me

form.unbind); . Suppose i form.submit (); I delete the line with , in other words, only form.unbind (); for the success case (if (Msg = "")) . When I first press the submit button, the form will normally make an asynchronous request to validate. If input is correct, (i.e. msg = "" ), when I press the submit button again, this standard will submit, because I have opened the submitted event. Therefore, the problem is not on that form.unbind (); , at least in my concern.

servlet code:

  Public Zero doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {PrintWriter = out response.getWriter (); String msg = ""; String name = request.getParameter ("name"); String email = request.getParameter ("email"); If ("". Equals (name)) {msg + = "
  • Please enter the name ; } If ("". Equals (email)) {msg + = "
  • Please enter email ; } If (title.length ()> gt; 20) {msg + = "
  • Name should be less than 20 characters. & Lt; / li & gt; ; } If (email.length ()> 30) {msg + = "
  • The title should be less than 30 characters. & Lt; / li & gt; ; } If (! "" .equals (msg)) {msg = "& lt; p> Error: ul & gt;" + Msg; Msg + = "& lt; / ul & gt;"; Out.print (MSG); } Out.close (); }

    Did you use $ .on () and $ .off () Have you tried to tie your events?

    something like

      $ (document) .ready (function () {$ ("# form"). ('Submit', function (event) {event. PreventDefault (); var frm = $ (this); var btn = $ ("# submit") button. Reuters ("disabled", "disabled"); $ .ajax ({url: type "valid searchlet" : "Post", data: frm.serialize (), success: function (msg) {if (msg == "") {frm.off ("submit"); frm [0]. Submit ();} and {Warning (msg); BT.N. removeAttr ("disabled");}}});});});   

    I am also trying to use 'form' as a 'variable' name

  • Comments

    Popular posts from this blog

    excel vba - How to delete Solver(SOLVER.XLAM) code -

    github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

    ios - Replace text in UITextView run slowly -