Jquery val () not working

jQuery val () did not work, this is a simple script:

$("#com_form").submit(function() { var name = $("#nama").val(); var komentar = $("#komentar").val(); alert.("Hi, "+name+" this is your comment: "+komentar) }); });*/ 

this is the HTML form:

 <form method="post" name="com_form" id="com_form"> <p>What is your name:<br> <input type="text" name="nama" id="nama"> </p> <p>Leave your comment here:<br> <input type="text" name="komentar" id="komentar"> </p> <p> <input type="submit" name="button2" id="button2" value="Submit"> </p> </form> 

Actually, I tried to create an ajax post, the value "nama" was sent, but not "komentar". So I tried to debug with a warning (as above), and yet "komentar" does not change. What should I do?

+6
javascript jquery
source share
4 answers

You have errors in your html. The form should have the attribute "action", also js is not very good.

I made examples of html and js files that work correctly. The html file passed the w3.org check and js passed jslint.

 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="http://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("jquery", "1.4.2"); </script> <script type="text/javascript" src="2988992.js"></script> <title>Insert title here</title> </head> <body> <form method="post" name="com_form" id="com_form" action="2988992.html"> <p>What is your name:<br> <input type="text" name="nama" id="nama"> </p> <p>Leave your comment here:<br> <input type="text" name="komentar" id="komentar"> </p> <p> <input type="submit" name="button2" id="button2" value="Submit"> </p> </form> </body> </html>
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="http://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("jquery", "1.4.2"); </script> <script type="text/javascript" src="2988992.js"></script> <title>Insert title here</title> </head> <body> <form method="post" name="com_form" id="com_form" action="2988992.html"> <p>What is your name:<br> <input type="text" name="nama" id="nama"> </p> <p>Leave your comment here:<br> <input type="text" name="komentar" id="komentar"> </p> <p> <input type="submit" name="button2" id="button2" value="Submit"> </p> </form> </body> </html> 
 "use strict"; /*global $, alert*/ $(function () { $("#com_form").submit(function () { var name = $("#nama").val(); var komentar = $("#komentar").val(); alert("Hi, " + name + " this is your comment: " + komentar); return false; //prevent standard behavior }); });
"use strict"; /*global $, alert*/ $(function () { $("#com_form").submit(function () { var name = $("#nama").val(); var komentar = $("#komentar").val(); alert("Hi, " + name + " this is your comment: " + komentar); return false; //prevent standard behavior }); }); 
+2
source share

Do you have another HTML element with id "komentar" elsewhere on the page?

+3
source share

to try

 $(document).ready(function() { //jquery code here } 
0
source share

Remove. after a warning so that he reads:

  alert("Hi, "+name+" this is your comment: "+komentar) 
0
source share

All Articles