skos
skos

Reputation: 4212

Jquery: Read Form Input Text Having Name As Associative Array

For some reason I have HTML like this -

<input type="text" value="100" name="ProductPrice[1][]">
<input type="text" value="200" name="ProductPrice[2][]">
<input type="text" value="300" name="ProductPrice[3][]">
<input type="text" value="400" name="ProductPrice[4][]">

And process this on server side like this -

foreach ($_POST['ProductPrice'] as $ProductId => $Price)
{
    //$Price = Price[0];
}

This works fine for me. However my problem is with validating this on client side with jquery.

I tried $.each($("input[name='ProductPrice[][]']"), function(key, value) { but nothing seems to be working. How can I read those input boxes using the NAME property.

Upvotes: 0

Views: 440

Answers (1)

Renato Zannon
Renato Zannon

Reputation: 29951

You can use the "Attribute Starts With Selector":

$("[name^=ProductPrice]").each(function() {
  var name  = $(this).attr("name");
  var value = $(this).val();

  // Do stuff
});

It will select all elements whose name starts with "ProductPrice"

Upvotes: 4

Related Questions