jquery选择器数组 [英] jquery selector array

查看:143
本文介绍了jquery选择器数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个添加到篮子问题。一些产品有选项,如颜色/大小。我绘制颜色/大小的选择框和qty的输入。

i am having an add to basket problem.Some products have options like color/size. I draw the select box for color/size and an input for qty.

<select name="productOption[1234][color]">
<select name="productOption[1234][size]">
<input type="text" name="productOption[1234][qty]">

so选项是保存每个产品的值的数组。产品id = 1234。

So option is an array that holds the values for each product.In the abov example are the options of product with id=1234.

我尝试了

var productOptions=$("name^='productOption["+productID+"]'").val();  

希望获得给定productID的选项数组,但它不工作。
这些选项是动态的一些产品可以有额外的选项

expecting to get the array of options for the given productID but its not working. The options are dynamically some products can have extra options

任何人都可以帮助我选择器,因此检索给定的productID值的数组,并将它们传递给serverSide 。

Can anyone help me with the selector so retrieve the array of values for given productID and pass them to serverSide.

感谢

推荐答案

您正在使用jQuery。

I think this is what you want assuming you're using 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>

这将返回一个关联的数组。所以你可以...

This will return an associated array. So you could do...

var details = getProductDetails( 1234 );
alert( details['qty'] );

这篇关于jquery选择器数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆