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?

3
  • 1
    Try using javascript objects Commented Oct 22, 2014 at 11:37
  • $('[name="employee[0][name]") s/b $("[name="+employee[0][name]+"]"), anyway, you need to use objects to achieve this. Commented Oct 22, 2014 at 11:37
  • stackoverflow.com/questions/9073690/… Commented Oct 22, 2014 at 11:38

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

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.