Jquery ui 我正在使用indexof(';custom';),它在1个条件下工作,但在另一个条件下失败
嗨,在我的购物车里,我放了一张itemname和carttotal的支票Jquery ui 我正在使用indexof(';custom';),它在1个条件下工作,但在另一个条件下失败,jquery-ui,jquery,jquery-plugins,Jquery Ui,Jquery,Jquery Plugins,嗨,在我的购物车里,我放了一张itemname和carttotal的支票 if (carttotal >'500' && itemname.indexOf("Custom") == 0) { //code to display popup } 在第1种情况下,它工作正常,而在其他情况下,它出现故障: 工作条件:如果我最后添加了itemname“custom”,则其工作: 例如: 购物车中的第一个项目是任何“abc”,最后一个项目名称是“cu
if (carttotal >'500' && itemname.indexOf("Custom") == 0)
{
//code to display popup
}
在第1种情况下,它工作正常,而在其他情况下,它出现故障:
工作条件:如果我最后添加了itemname“custom”,则其工作:
例如:
购物车中的第一个项目是任何“abc”,最后一个项目名称是“custom”,carttotal>500。然后弹出显示良好
不工作情况:如果我先添加itemname“custom”,然后再添加一些类似“xyz”的项目。carttotal大于500。则不会显示弹出窗口。您当前的代码检查
custom
是否是列表中的第一项(索引为0
)。要检查列表中是否存在Custom
,您真正想做的是查看indexOf('Custom')
是否返回-1
,因为-1
是indexOf()
的失败值,而不是0或其他错误值。例如:
var carttotal = /* Your cart total */
var products = [ /* Lots of products */ ];
for(var i = 0; i < products.length; i++) {
itemname = products[i];
if(carttotal > 500 && products.indexOf('Custom') > -1) { // `products`, not `itemname`
// Popup
}
}
var cartotal=/*您的购物车总数*/
var产品=[/*大量产品*/];
对于(变量i=0;i500&&product.indexOf('Custom')>-1){/`products',而不是`itemname`
//弹出窗口
}
}
这里,我还检查了
产品
,假设这是您的产品阵列。如果itemname
为,则使用它。不是第一项0
的索引吗?这意味着您的if
条件将失败,如果“Custom”是第一项。itemname价格第二项)abc 100第一项)Custom 600 Cart总计700不起作用。条件2:项目名称价格第二项)定制600第一项)abc 100 Cart总计700件工作正常。项目名称价格第二项)abc 100第一项)定制600Cartotal 700不起作用。条件2:商品名称价格第二项)定制600第一项)abc 100 Cart总计700工作正常。更好地解释您的问题。很难从你的描述中找出正确的行为。我的理解是,如果custom
不在列表中,并且cartotal>500
我只希望在列表中有“custom”时出现弹出窗口。现在发生的情况取决于我的情况。如果我第一次添加了名为“abc”的任何项目,最后添加了itemname“custom”和cartotal>500,这种情况下工作。现在仔细检查这个条件:如果我第一次添加了任何项目“custom”,然后最后添加了itemname“abc”,并且carttotal>500,则不会显示弹出窗口。您现在清楚了吗?谢谢,汉克斯,事情都解决了。请看我的编辑-希望这次答案是正确的Hi Bojangles它不工作请检查我的代码。var itemname=$('#Hidden9').val();var compare_value_oldd=“$500”;var compare_value_neww=parseFloat(compare_value_oldd.replace(/[^0-9-.]/g',);对于(var i=0;i