jQuery val() didnt working, this is the 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 was tried to create ajax post, the value "nama" is submited but not "komentar". So I tried to debug using alert (like one above) and still "komentar" is not change. What should I do?
Do you have another HTML element with the id "komentar" elsewhere on the page?
You have some errors in your html. Form should have "action" attribute, also js is not well.
I made example html and js files which works correctly. Html file passed w3.org validation 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>
"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
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With