【发布时间】:2016-07-14 04:24:25
【问题描述】:
我们已在 Shopify 的“添加到购物车”表单中添加了一些自定义字段,并正在尝试添加一些验证。我们已经编写了自定义验证函数,并尝试使用以下代码绑定它,我们将其放置在 body 标签内的 theme.liquid 中:
jQuery(function($) {
$('#AddToCartForm').submit(function() {
console.log("Validation called");
return false;
});
});
我们修改了“添加到购物车”表单,使其具有 AddToCartForm id。但是,永远不会调用此函数。仅记录 $('#AddToCartForm') 表明选择器工作正常。我们还尝试将其移至产品液体页面,使其位于表单关闭标签下方,但这并没有改变任何东西。我们还尝试绑定到提交按钮本身,然后使用
e.preventDefault();
e.stopPropagation();
阻止表单提交,但这也不起作用。但是,它确实绑定并且函数被正确调用,它只是不会停止提交。我们整天都在尝试解决这个问题,并且完全没有想法,非常感谢任何帮助。
编辑:
我们其实也尝试过使用onSubmit,所以代码是:
<form onSubmit='return test()'>
js 是:
function test() {
console.log("Validation called");
return false;
}
这个函数也从未被调用过。
我们尝试的另一件事是将添加到购物车按钮更改为:
<button type="button" name="add" id="AddToCart" class="btn">
来自
<button type="submit" name="add" id="AddToCart" class="btn">
然后调用 $('#AddToCartForm').submit() 如果验证通过,主要问题是有人可以从控制台调用 $('#AddToCartForm').submit() 来提交表单并绕过所有验证..
我们当前的验证脚本是:
jQuery(function($) {
$('#AddToCart').click(function(e) { //Changed this to the ID of the submit button and changed .submit to .click
console.log(e);
e.preventDefault();
e.stopPropagation();
var formIsValid = true;
var message = "Please fill this out and you will be able to add the item to your cart.";
/* Changed this to the ID of the form itself */ $('#AddToCartForm').find('[name^="properties"]').filter('.required, [required="required"]').each(function() {
$(this).removeClass('error');
if (formIsValid && $(this).val() == '') {
formIsValid = false;
message = $(this).attr('data-error') || message;
$(this).addClass('error');
}
});
if (formIsValid){
$('#AddToCartForm').submit();
return true;
}
else {
alert(message);
return false;
}
}).find('input, select, textarea').focus(function() {
$(this).removeClass('error');
});
});
它基于:https://help.shopify.com/themes/customization/products/get-customization-information-for-products#require-fields 和 https://gist.github.com/carolineschnapp/11167400
shopify 表单的 HTML 是:
<form action="/cart/add" method="post" enctype="multipart/form-data" id="AddToCartForm">
<select name="id" id="productSelect" class="product-single__variants">
{% for variant in product.variants %}
<option {% unless variant.available %} disabled="disabled" {% endunless %} {% if variant == current_variant %} selected="selected" {% endif %} data-sku="{{ variant.sku }}" value="{{ variant.id }}">{{ variant.title }} - {% if variant.available %}{{ variant.price | money_with_currency }}{% else %}{{ 'products.product.sold_out' | t }}{% endif %}</option>
{% endfor %}
</select>
<div class="product-single__quantity{% unless settings.product_quantity_enable %} is-hidden{% endunless %}">
<label for="Quantity">{{ 'products.product.quantity' | t }}</label>
<input type="number" id="Quantity" name="quantity" value="1" min="1" class="quantity-selector">
</div>
<button type="submit" name="add" id="AddToCart" class="btn">
<span id="AddToCartText">{{ 'products.product.add_to_cart' | t }}</span>
</button>
</form>
【问题讨论】:
-
将“提交”更改为“按钮”并使用
.on('click',function()...。验证后使用$('#form_id').submit(). -
这行得通,但它不再是像以前添加到购物车那样的 AJAX 调用。知道为什么吗?
-
最重要的是,不幸的是有人可以在控制台中输入 $('AddToCartForm').submit() 来绕过验证,关于如何解决这个问题的任何想法?
-
可以分享完整的表单代码和完整的验证功能吗?
-
我已将其添加到原帖中
标签: javascript jquery shopify