【发布时间】:2014-11-22 04:31:30
【问题描述】:
我在 Shopify 的页面模板中使用链接列表,我需要将其长度传递给 javascript 代码段。我不知道该怎么做。
页面模板包括这一行:
{% assign linklist = linklists['link-list-1'] %}
并且该页面包含一个 id="submit-table" 的表单,类似于此代码:
<form>
<input type="submit" value="Add..." id="submit-table"/>
...
</form>
首先,我需要包含链接列表长度值的表单。不知道该怎么做。
然后我需要将该长度值传递给这个脚本(这是一个片段)。另外,不知道该怎么做。
<script type="text/javascript" charset="utf-8">
//<![CDATA[
// Including jQuery conditionnally.
if (typeof jQuery === 'undefined') {
document.write({{ "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" | script_tag | json }});
document.write('<script type="text/javascript">jQuery.noConflict();<\/script>');
}
//]]>
</script>
<script>
$(document).ready(function () {
$("#quantity-0").focus();
$("#submit-table").click(function(e) {
e.preventDefault();
var toAdd = new Array();
var qty;
//I need the link list length value here
for(i=0; i < length; i++){
toAdd.push({
variant_id: $("#variant-"+i).val(),
quantity_id: $("#quantity-"+i).val() || 0
});
}
function moveAlong(){
//I need the link list length value here
if (toAdd.length) {
var request = toAdd.shift();
var tempId= request.variant_id;
var tempQty = request.quantity_id;
var params = {
type: 'POST',
url: '/cart/add.js',
data: 'quantity='+tempQty+'&id='+tempId,
dataType: 'json',
success: function(line_item) {
//console.log("success!");
moveAlong();
},
error: function() {
//console.log("fail");
moveAlong();
}
};
$.ajax(params);
}
else {
document.location.href = '/cart';
}
};
moveAlong();
});
});
</script>
脚本包含在我的 theme.liquid 代码中:
{% include 'order-form-script' %}
参考资料:
techi 博客 » Shopify 教程:如何创建订单
http://www.tetchi.ca/shopify-tutorial-order-form/#comment-10129
【问题讨论】:
标签: javascript html forms shopify liquid