0

I'm having trouble getting .each() function to work with an array of textboxes. I can't seem to figure out what I'm missing.

If I used $("input[type=text]").each() it works fine.

If I used $("[name=age[]]").each() it doesn't work.

<?php 
  //this code is abbreviated
  foreach ($dbresult as $obj) {$output = "<input type='text' name='age[]' value='" . $obj['age'] . "'";}
?>
$(document).ready(function () {
    $("#form").submit(function () {
        $("[name=age[]]").each(function () {
            if (!$.isNumeric($(this).val())){return false;}
        return true;});
});
1
  • Post your rendered HTML, not the PHP. Commented Oct 26, 2013 at 13:06

4 Answers 4

4

You need to esacpe [], thus change your selctors as

 $("[name=age\\[\\]]")

DOCS

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \.

Or you can use Attribute Starts With Selector [name^="value"], like

$("[name^=age]")
Sign up to request clarification or add additional context in comments.

Comments

1

Try This

$("input[name='age[]']").each()

Comments

0

if you are using array you have to try following code

$.each( yourarrayname, function( key, value ) {
  alert( key + ": " + value );
});

Comments

0

$("[name=age[]]") doesn't seem to be a valid selector. Try $("[name^=age]") instead.

1 Comment

This doesn't work if you have also other fields with names starting with the letters "age", for example "agelimit"

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.