Jquery change function don’t work properly

Posted by user1493448 on Stack Overflow See other posts from Stack Overflow or by user1493448
Published on 2012-10-08T21:32:42Z Indexed on 2012/10/08 21:37 UTC
Read the original article Hit count: 442

I have same id, name two html select. If I change first html select then it’s work properly but If I change second html select then it’s not work. Please can someone point out what I may be doing wrong here? Many thanks. Here is my code:

<html>
<head>
<title>the title</title>
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
   <script type="text/javascript" language="javascript">
  $(document).ready(function() {
      $("#name").change(function(){
          $.post(
             "data.php",
             $("#testform").serialize(),
                 function(data) {
                $('#stage1').html(data);
             }
          );
          var str = $("#testform").serialize();
          $("#stage2").text(str);
      });
   });
   </script>
</head>
<body>
   <div id="stage1" style="background-color:blue; color: white">
          STAGE - 1
   </div>

<form id="testform">
 <table>
 <tr>
 <td><p>Fruit:</p></td>
 <td>
     <select id="name" name="name[]">
         <option>Apple</option>
         <option>Mango</option>
         <option>Orange</option>
         <option>Banana</option>
     </select>
 </td>
</tr>
 <tr>
 <td><p>Fruit:</p></td>
 <td>
     <select id="name" name="name[]">
         <option>Apple</option>
         <option>Mango</option>
         <option>Orange</option>
         <option>Banana</option>
     </select>
 </td>
</tr>
</table>
</form>
</body>
</html>

Php code:

<?php
$fruit=$_REQUEST["name"];
$n = count($fruit);


for($i=0;$i<$n; $i++)
{
    echo $fruit[$i]."<br/>";
}
?>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery