jquery - json jqmodal populate dropdown -


i'm trying populate dropdown in jqmodal different product sizes. can't work. appreciated.

my json

{ "product": { "variants": { "3394248": { "id": 3394248, "code": "19", "ean": "19", "sku": "19", "price": { "price": 19.95, "price_incl": 23.7405, "price_excl": 19.95, "price_old": 0, "price_old_incl": 0, "price_old_excl": 0 }, "title": "maat 95", "active": false }, "3376388": { "id": 3376388, "code": "19", "ean": "19", "sku": "19", "price": { "price": 19.95, "price_incl": 23.7405, "price_excl": 19.95, "price_old": 0, "price_old_incl": 0, "price_old_excl": 0 }, "title": "maat 100", "active": true } }, } } 

my script

<script type="text/javascript"> $('#productvariations').jqm({ trigger: 'a.ex2trigger' }); function loadproductvariations(){ var productid = '{{ product.fulltitle }}'; var url = 'http://shop.com/'+productid+'/?format=json'; $.getjson(url,function(data){ var varianthtml = ''; $.each(data.product.variants, function(index, variants){ varianthtml = varianthtml + '<option value="' +variants.id+'</option>'; }); $('#productoptions').html(varianthtml); }); } 

my html

<div class="jqmwindow" id="productvariations"> geduld aub... <img src="https://shop.com/com/ajax-loader.gif?1" alt="loading" /> <span id="close"> <button class="jqmclose">close</button> </span> <br/><br/> <form class="formproduct" id="product_configure_form" method="post" action="{{ ('cart/add/' ~ product.vid) | url }}" enctype="application/x-www-form-urlencoded"> <div id="productoptions"> <select onchange="document.getelementbyid('product_configure_form').action = 'http://shop.com/product/variants/2140679/'; document.getelementbyid('product_configure_form').submit();" id="product_configure_variants" name="variant"> </select> </div> <a id="submit" class="button gray" href="#" onclick="$('#product_configure_form').submit(); return false;" title="{{ 'add cart' | t }}">{{ 'add cart' | t }}</a> </form> <span id="message"></span> 

this concept forgive me current mistakes in code! thing need know how populate dropdown box.

your json not correctly formatted, one. missing 2 closing brackets, have edited in original post.

second, try changing loop so:

$.each(data.product.variants, function(index, variants){ varianthtml = varianthtml + '<option value="' +variants.id+'</option>'; }); 

to

for (var in data.product.variants) { var v = data.product.variants[i]; var $opt = $("<option>").text(v.title); $("#productoptions select").append($opt); } 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -