通过动态创建的按钮URL传递动态文本输入值?

时间:2013-10-28 08:53:05

标签: jquery jquery-mobile parameters dynamic-data

您好,我点击了添加到购物车按钮后创建了有关特定产品的动态页面 我想发送文本框值>我创建了像

这样的页面
$(document).on('pageshow','#productdetails', function() {
    var pid = getURLParameter('pid');
    $.getJSON("http://vinoth.com/magento/api/rest/products/"+pid, function(data) {
        if (data.is_in_stock == "1") {
            var stock = "In Stock"
        }
        else {
            var stock = "Out of Stock"
        }

        //var imageurl = 'http://vinoth.com/magento/api/rest/products/'+data.entity_id+'/images'; 
        // $.getJSON(imageurl,function(result){
        //  $.each(result, function(j, k) {

        $("div[data-role='content']").append('<h4>'+data.name+'</h4><img src=' + data.image_url + ' width="100%"><br><p><strong>Description: </strong>' + data.description + '</p><span><strong>Actual Price: </strong>' + data.regular_price_with_tax + ' INR</span><br><span><strong>Special Price: </strong>' + data.final_price_with_tax + ' INR</span><br/> <span><strong>Availablity: </strong>' + stock + '</span><br><div data-role="fieldcontain"><label name="quantity"><strong>Qty: </strong></label><input type="text" name="quantity" value="" data-mini="true" data-inline="true" size="30" id="qty"/></div>').trigger('create');

        var qty = $("#qty").val();


        $("div[data-role='content']").append('<a data-role="button" data-mini="true" data-inline="true"  data-icon="plus" href="checkoutcart.html?pid=' + data.entity_id + '&quantity=' + $("#qty").val() + '" >Add to Cart</a>').trigger('create');

        //});
        // });
    });
});

如何获取qty输入值以通过addtocart按钮链接?

2 个答案:

答案 0 :(得分:0)

首先确保qty始终具有值。

请使用data- *属性不传递查询字符串中的值。

$("div[data-role='content']").append('<a data-role="button" data-pid="' + $('#qty').val() + " data-mini="true" data-inline="true"  data-icon="plus" href="checkoutcart.html?pid=' + data.entity_id + '&quantity=' + $("#qty").val() + '" >Add to Cart</a>').trigger('create');

点击“添加到购物车”,使用此选项可获取所需的pid值:

$(this).data('pid');

答案 1 :(得分:0)

如果我理解您的问题,我认为您应该为添加到购物车链接编写点击处理程序,然后在点击时构建URL,以便您可以获取用户编辑的数量。目前,您正在将URL编码为创建“添加到购物车”按钮时的数量。

因此,在附加链接时,给它一个ID,并将pid存储在Sheetal建议的数据中:

$("div[data-role='content']").append('<a id="addtocart" data-pid="' + data.entity_id + '" data-role="button" data-mini="true" data-inline="true"  data-icon="plus" href="#" >Add to Cart</a>').trigger('create');

然后添加处理程序:

$('#addtocart').on('click', function(){
    var url = checkoutcart.html?pid=' + $(this).data("pid") + '&quantity=' + $("#qty").val() + '
    $.mobile.changePage( url);//  or  location.href = url;
});

如果我不明白这个问题,请创建一个演示它的小jsFiddle ......