There is a piece of code where blocks appear in the for loop, as well as inside another for loop that displays the values ​​of the option and value fields. When adding a product, only the first value is taken, that is, you select, for example, 6, press "add to cart", and the figure 1 always comes.

 $(document).ready(function(){ $('.name a').click(function(){ $this = $(this); var id = $this.parent().find('select').attr('id'); var count = $this.parent().find('option').val(); alert(count); $.ajax({ type: 'POST', url: 'post.php', data: {itemid: id, count: count}, success: function(data){ $this.parent().find('.add').html('Товар добавлен!'); } }); }); }); 
 <?for($i = 1; $i <= 9; $i++){?> <div id="wripper"> <div class="name"> Товар №-<?=$i?> <img src="images/img.png" alt=""> <select id="item<?=$i?>"> <?for($q = 1; $q <= 9; $q++){?> <option value="<?=$q?>"><?=$q?></option> <?}?> </select> <a href="javascript:void(0);">Добавить товар</a> <div class="add"></div> </div> </div> <?}?> 

    1 answer 1

     <!DOCTYPE html> <html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> </head> <body> <?for($i = 1; $i <= 9; $i++){?> <div id="wripper"> <div class="name"> Товар №- <?=$i?> <img src="images/img.png" alt=""> <select id="item<?=$i?>"> <?for($q = 1; $q <= 9; $q++){?> <option value="<?=$q?>"><?=$q?></option> <?}?> </select> <a href="javascript:void(0);">Добавить товар</a> <div class="add"></div> </div> </div> <?}?> <script type="text/javascript"> $(document).ready(function() { $('.name a').click(function() { $this = $(this); var id = $this.parent().find('select').attr('id'); var count = $("#" + id).val(); alert(count); $.ajax({ type: 'POST', url: 'post.php', data: { itemid: id, count: count }, success: function(data) { $this.parent().find('.add').html('Товар добавлен!'); } }); }); }); </script> </body> </html> 
    • What does this record mean? $ ("#" + id) .val (); What is a grill? add ID and value value to grid - Vladimir Gunaev
    • # - means ID (analogue of document.getElementByID ('id')). That is, get the item by his ID. #item = <div id = "item"> ... </ div>. $ ("#" + id) .val () - get the value (in your case, the value of the selected item). - ikerya