0

I have a form like this :

<form>
  <input type="text" name="employee[][name]"/>
  <input type="text" name="employee[][name]"/>
</form>

And I use the result in a php file like this :

echo $_POST['employee'][0]['name'];
echo $_POST['employee'][1]['name'];

The fields are converted in arrays. It's a dynamic form, I can add or remove some fields so it is usefull to not have a fixed name for each fields.

I want to do the same thing in Javascript/JQuery. Is tried to do $('[name="employee[0][name]") and it return an empty array.

I don't know what to do. Is there a simple way to do this?

Dougui
  • 7,142
  • 7
  • 52
  • 87

1 Answers1

3
<html>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
$(document).ready(function(){
    $('input[name^="employee[name]"]').each(function() {
            alert($(this).val());
    });
});
</script>
<body>
<form>
  <input type="text" name="employee[name][]" value="value1"/>
  <input type="text" name="employee[name][]" value="value2"/>
</form>
</body>
</html>
Vipin
  • 847
  • 1
  • 10
  • 21