Javascript 尝试将querystring中的值分配给Shopify行项目属性
HTML }Javascript 尝试将querystring中的值分配给Shopify行项目属性,javascript,html,query-string,shopify,liquid,Javascript,Html,Query String,Shopify,Liquid,HTML } 这里javascript从查询字符串中提取一个名为side1的值。那里的console.log实际上最终打印了我想要的结果,但由于某些原因,它似乎没有保存在line item属性中。结果表明,在单击add to cart时运行该函数是个坏主意,因为它首先发送表单数据,然后在,给我留下了一个不想要的结果。我只是通过body标签的onload事件调用了这个函数,现在它可以工作了 <div> <input type="hidden" id="shirtImg"
这里javascript从查询字符串中提取一个名为side1的值。那里的console.log实际上最终打印了我想要的结果,但由于某些原因,它似乎没有保存在line item属性中。结果表明,在单击add to cart时运行该函数是个坏主意,因为它首先发送表单数据,然后在,给我留下了一个不想要的结果。我只是通过body标签的onload事件调用了这个函数,现在它可以工作了
<div>
<input type="hidden" id="shirtImg" name="properties[ShirtImg]"/>
</div>
<div id="purchase" onclick="createLineItemProperties()">
<p class="price"></p>
<input class="btn" type="submit" name="add" id="add-to-cart" value="Add to Cart" />
</div>
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
vars[key] = value;
});
return vars;
function createLineItemProperties() {
if(getUrlVars()["side1"] != null){
console.log("inside if statement");
document.getElementById("shirtImg").value = getUrlVars()["side1"];
}
console.log(document.getElementById("shirtImg").value);
}