0

I am using serialize() method in my JSP page. But its returning null. Dont know what I am doing wrong Please help.

jQuery Code :

$(document).ready(function() {

  $("#addBtn").click(function() {
      var offerData = $("#testForm").serialize();
      alert(offerData);
  }

});

HTML Code :

<form id="testForm">
  <table>
    <tr>
      <td>Name :</td>
      <td>
        <input type="text" name="name" id="name" />
      </td>
    </tr>
    <tr>
      <td>Address :</td>
      <td>
        <input type="text" name="address" id="address" />
      </td>
    </tr>
    <tr>
      <input type="button" id="addBtn" />
    </tr>
  </table>
</form>

I posted relevant code only.

3
  • 1
    Closing bracket alert(offerData); }); Commented Jan 15, 2016 at 17:38
  • Sane indentation would show the error almost immediately. Commented Jan 15, 2016 at 17:48
  • could you once replace input type button to submit and try. If not working then also then try using the form id selector and post the response :) Commented Jan 15, 2016 at 18:29

4 Answers 4

1

I think your javascript code is bad formatted, I tried this and it works fine...

$(document).ready(function () {

      $("#addBtn").click(function () {
          var offerData = $("#testForm").serialize(); 
          alert(offerData);
      });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<form id="testForm">
<table>
<tr>
 <td>Name :</td>
 <td><input type="text" name="name" id="name"/></td>
</tr>
<tr>
 <td>Address :</td>
 <td><input type="text" name="address" id="address"/></td>
</tr>
<tr>
<input type="button" id="addBtn"/>
</tr>
</table>
</form>

Sign up to request clarification or add additional context in comments.

Comments

1

There's an error in your javascript code. It should be:

<script type="text/javascript">
  $(document).ready(function () {

      $("#addBtn").click(function () {
          var offerData = $("#testForm").serialize(); 
          alert(offerData);
          });
         });
</script>

Notice the closing bracket and semicolon after your click method.

Comments

1

you forgot the close parenthesis, the code below works

 $(document).ready(function () {
    $("#addBtn").click(function () {
      var offerData = $("#testForm").serialize(); 
      alert(offerData);
    });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<form id="testForm">
<table>
<tr>
 <td>Name :</td>
 <td><input type="text" name="name" id="name"/></td>
</tr>
<tr>
 <td>Address :</td>
 <td><input type="text" name="address" id="address"/></td>
</tr>
<tr>
<input type="button" id="addBtn"/>
</tr>
</table>
</form>

Comments

1

Missing closing parenthesis for click event handler. Also in the HTML the last <tr> has no <td> included. Consider including your input element into <td colspan="2">

jQuery

  $("#addBtn").click(function() {
      var offerData = $("#testForm").serialize();
      alert(offerData);
  }); // <-- missing );

HTML

<tr>
  <td colspan="2">
    <input type="button" id="addBtn" />
  </td>
</tr>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.