我想知道下面這行是否需要在這個腳本中,如果是的話,它的用途是什麼。什麼是jQuery focus()在此代碼中執行的方法?
$("#quantity-0").focus();
如果我沒有表單域ID爲「量-0」,我可以專注什麼其他元素(如果需要的話)?我可以關注隱藏的表單元素嗎?
這是我的代碼。它來自this blog。
<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();
var length = $("#linklist-length").val();
$("#submit-table").click(function(e) {
e.preventDefault();
//array for Variant Titles
var toAdd = new Array();
var qty;
for(i=0; i < length; i++){
toAdd.push({
variant_id: $("#variant-"+i).val(),
quantity_id: $("#quantity-"+i).val() || 0
});
}
function moveAlong(){
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>
我給出的鏈接(http://www.tetchi.ca/shopify-tutorial-order-form/)描述了所有這些。該腳本將商品添加到購物車。 – MountainX 2014-11-23 03:02:52