嗨,我在为购物车编写代码

$('.addtoCart').click(function() {

    //get button id
    var cartID = $(this).attr("id");

    //get check box id
    var checkBoxID = $('input.cartLevelChecked:checked').attr("id");

    //get parent id
    var levelListID = $(this).closest('li').attr("id");

    if(('#'+cartID && '#'+checkBoxID).parent('#'+levelListID)){
        $(".selectPlan").show();
    }

    //alert(/*cartID + checkBoxID +*/ levelListID);
});

基本上我在检查用户选中的复选框和他们单击的按钮是否属于同一个父项,然后显示一个div
任何帮助都将不胜感激。谢谢

最佳答案

您需要将正确查询的levelListID与复选框最接近的li父级的id进行比较,您应该以相同的方式进行查询:

if (levelListID === $('#' + checkBoxID).closest('li').attr('id')) {
    ...
}

10-08 17:19