Using .each() to make multiple posts
Hey all,
I am working on a form that has multiple divs that have similar inputs. These are dynamically generated but have a structure similar to this :
- <div class = "item">
- <input name = "name" class = "name" type = "text" />
- <input name = "price" class = "price" type = "text" />
- </div>
- <div class = "item">
- <input name = "name" class = "name" type = "text" />
- <input name = "price" class = "price" type = "text" />
- </div>
- <div class = "item">
- <input name = "name" class = "name" type = "text" />
- <input name = "price" class = "price" type = "text" />
- </div>
- <input type = "submit" id = "submit" value = "Save" />
Now what I would like to do is do post to a php file for each "item" div and use the values from their children, I want this to happen when the submit button is pushed. Something like this :
- $("#submit").click(function(){
- //For each item div
- $(".item").each(function(){
- var data = "name="+$(this).find(".name").val(); // add the name to the query string
- data += "&price"+$(this).find(".price").val(); // add the price to the string
- $.post('submit.php',data,function(data){
- //do something with the data if I want to
- }
- });
- });
My questions are is it even possible to use .each like this? If not is there another way to do this? Thanks much for any advice or suggestions, it is very much appreciated.