Javascript jQuery使用each循环获取第四个或更多数量的复选框的值

Javascript jQuery使用each循环获取第四个或更多数量的复选框的值,javascript,jquery,Javascript,Jquery,我正在运行一个表单的each循环,根据用户选择对值求和,并获得产品的最终价格。 困难的部分是,在特定的复选框组中,我有进一步的限制,例如:在单击4个或更多复选框之后,组A将开始添加到价格中,并且仅添加在第3项之后单击的复选框的值 下面是我创建的函数,我正在寻找解决问题的方法: var quantity = 1; var removedProductPrice = false; //User can pick a variation of the same product, so the pric

我正在运行一个表单的each循环,根据用户选择对值求和,并获得产品的最终价格。 困难的部分是,在特定的复选框组中,我有进一步的限制,例如:在单击4个或更多复选框之后,组A将开始添加到价格中,并且仅添加在第3项之后单击的复选框的值

下面是我创建的函数,我正在寻找解决问题的方法:

var quantity = 1;
var removedProductPrice = false; //User can pick a variation of the same product, so the price changes to that selected variation (flag)    
var product_price = parseFloat($("#product").val()); //Base product price
var totalProduct = product_price;

$(":input", "#productForm").each(function(){
    if(this.type == 'checkbox' || this.type == 'radio'){
        if(this.checked == true){

            priceProtocol = $(this).attr('rel').split('_');
            //priceProtocol[0]: 'variation/component/steady, priceProtocol[1]: number of choices

            switch(priceProtocol[0]){               
                case 'variation':
                    removedProductPrice = true;
                    totalProduct = Number(totalProduct)+Number(this.value);
                break;

                case 'component':                       
                    totalProduct = Number(totalProduct)+Number(this.value);                     
                break;          

                case 'steady':                      
                    //If the number of clicked checkboxes is greater than the limit, start adding to the price
                    if($("input[name='"+this.name+"']:checked").length > priceProtocol[1]){                         
                        totalProduct = Number(totalProduct)+Number(caller.val());
                    }
                break;  
            }

        }
    }else if(this.id == 'product_quantity'){ quantity  = this.value; }
});

//If a variation selected, change the price to that of the variation
if(removedProductPrice){ totalProduct = totalProduct-product_price; }

//Multiple by the selected quantity
totalProduct = Number(quantity)*totalProduct;

    //Preview the final price
$("#current_product_price").html(totalProduct.toFixed(2));  
 }

如何实现上述目标?

如果仅在选中4个或更多复选框后添加价格,则可以使用选择器:

if ($("#GroupAId option:selected").length > 4) {
...
}
对于仅在第三个项目之后添加单击的项目,您可以使用该方法。仅添加索引>2的项目(从零开始)

    foo 酒吧
  • baz
... var listItem=document.getElementById('bar'); 警报('Index:'+$('li').Index(listItem)); //我们返回列表项的从零开始的位置: //将发出警报-索引:1

希望这有助于

RKumsher我已经检查了添加值的条件是否合适:if($($)input[name=”+this.name+“]:checked”).length>priceProtocol[1])问题是如何只添加在第3项(第4项、第5项、第6项等)之后单击的内容。在每次选中时,您将复选框添加到有序列表中,在每次取消选中时,您将删除复选框。然后在计算中,只考虑索引为3或更大的复选框。希望我能正确理解你的问题是的,但它可能是两组以上的复选框,在表格中具有相同的特征。例如:从第一组开始,当用户单击第四个复选框项时,汇总开始,而从第二组开始,当用户单击第二个复选框项时。您能给我一个使用每个组的有序列表的代码示例吗?此函数在单击事件时触发。此外,还会动态创建复选框组。
<ul>
  <li id="foo">foo</li>
  <li id="bar">bar</li>
  <li id="baz">baz</li>
</ul>

...

var listItem = document.getElementById('bar');
alert('Index: ' + $('li').index(listItem));
//We get back the zero-based position of the list item:
//Would alert - Index: 1