Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery val() not working

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?

like image 252
Vina Avatar asked Jun 07 '10 11:06

Vina


2 Answers

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

like image 127
David M Avatar answered Oct 06 '22 00:10

David M


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
  });
});

like image 39
sbmaxx Avatar answered Oct 06 '22 01:10

sbmaxx