我遇到添加到购物篮的问题。有些产品有颜色/尺寸等选项。我绘制了颜色/尺寸的选择框和数量的输入。
<select name="productOption[1234][color]">
<select name="productOption[1234][size]">
<input type="text" name="productOption[1234][qty]">
所以 option 是一个数组,保存每个产品的值。上面的例子是 id=1234 的产品的选项。
I tried
var productOptions=$("name^='productOption["+productID+"]'").val();
期望获取给定产品 ID 的选项数组,但它不起作用。
选项是动态的,某些产品可以有额外的选项
任何人都可以帮助我使用选择器,以便检索给定 ProductID 的值数组并将它们传递到服务器端。
Thanks
我认为假设您使用的是 jQuery,这就是您想要的。
<script type="text/javascript">
function getProductDetails( product_id ) {
var product_details = [];
jQuery( '[name*="productOption[' + product_id + ']"]' ).each( function() {
var option = jQuery( this );
var type = option.attr( 'name' ).match( /productOption\[\d+\]\[(\w+)\]/ );
product_details[ type[1] ] = option.val();
} );
return product_details;
}
</script>
这将返回一个关联的数组。所以你可以做...
var details = getProductDetails( 1234 );
alert( details['qty'] );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)