基本购物车 - 为什么我的单选按钮状态引用不正确?

Basic shopping cart - Why is my Radio button state referenced incorrectly?

我在 jQuery 中内置了一个非常简单的 "shopping cart"。 (全部在一页上)。 您可以通过选中复选框来添加项目,并通过取消选中复选框来删除它们。但是,复选框的状态未被正确引用,因此我无法输入下面 if else 语句的 "add to cart" 部分:

$('#coreSetM').click(function()
{ 
    // Get the value of radio button
    var theMValue = $(this).attr("value");
    var theMPackage = $(this).attr("id");
    var theChecked = $(this).is(':checked');

    console.log("Value:"+theMValue+"\nPackage:"+theMPackage+"\nCheck Value:"+theChecked);

    if(theChecked==="false")
    {
        alert("False: "+theChecked);
        //do some add to cart stuff here with innerHTML
    }
    else
    {
        alert("True: "+theChecked)
        //do some remove from cart stuff here with innerHTML
    }
});

尽管控制台记录了以下内容:-

Value:118
Package:coreSetM
Check Value:false 

Value:118
Package:coreSetM
Check Value:true

它永远不会进入循环的第一部分...我不知道为什么,因为 theChecked 的值是 "false"。 (在控制台输出中确定。)这是为什么?

三等号是一种强比较,它也比较变量的类型。在您的代码中,您将布尔值与字符串进行比较。你的 if 语句总是会导致 false

就这样:

if(!theChecked)
{
    alert("False: "+theChecked);
    //do some add to cart stuff here with innerHTML
}
else
{
    alert("True: "+theChecked)
    //do some remove from cart stuff here with innerHTML
}

缺少结尾 )

编辑:您正在使用 theChecked === 'false' 将布尔值与字符串进行比较。将其切换为 theChecked === false,你应该没问题

已更新 fiddle:https://jsfiddle.net/t6rfwbaf/