javascript - Make a submit button outside the form element - isn't working -


I want to create a button outside the form element which will check whether the inputs have been filled or not, if they are, So submit the form, if not, do something else (like a warning or some things to inspire).

I have made this bela, and it will not work:

HTML:

  & lt; Form action = "javascript: alert ('submit');" Method = "post" & gt; & Lt; Input type = "text" id = "inp_name" & gt; & Lt; Input type = "text" id = "inp_address" & gt; & Lt; Input type = "text" id = "inp_zipcode" & gt; & Lt; Input type = "text" id = "inpir_city" & gt; & Lt; Input type = "submit" value = "submit form" & gt; & Lt; / Form & gt; & Lt; A href = "#" id = "to-top" & gt; I have a fire FORM FORM element & lt; / A & gt; Click to submit  

Javascript:

  var bas_name = $ ("#inp_name"). Val (); Var bas_address = $ ("#inp_address") Val (); Var bas_zipcode = $ ("#inp_zipcode"). Val (); Var bas_city = $ ("# inp_city") Val (); $ ("#s-top") Click (function () {if (bas_name == "" & amp; amp; amp; amp; base_address == "" & amp; amp; amp; andbiz_zipcode == "" & amp; ampbarcover == ") {Warning (" nonroneno! ");} And {$ (" form "). Submit ();}});  

Here is the issue variable scope: You can do this: -

 Click  $ ("# to-top") ( Function () {var bas_name = $ ("#inp_name") .val (); var bas_address = $ ("#inp_address") .val (); var bas_zipcode = $ ("# inp_zipcode"). Val (); var Basak = $ ("# inp_city"). Val (); if (bas_name == "" & amp; amp; amp; and; base_address == "" & amp; amp; amp; amp; andbas_zipcode = = "" & Amp; nbsp; P;; amp; amp; bankorter == "") {Warning ("nonoonnono!");} And {$ ("form"). Submit ();}});  
This problem has occurred since

, you have placed all the variables outside the click event. Therefore, it will always store empty values ​​on page load and thus your code will not work. However, When clicking on anchor tag, you will need to get existing values ​​in the textbox and then do all the assumptions.

Hope, you got me!


Comments