Javascript “输入时发出警报”字段;类型=编号;在simplecart中添加项时为空
我试图在simplecart中的项目上添加错误参数时获得警报。 当字段为空或如果其值低于400或高于500时 一切正常,但字段为空时警报不起作用 Html:Javascript “输入时发出警报”字段;类型=编号;在simplecart中添加项时为空,javascript,jquery,simplecart,Javascript,Jquery,Simplecart,我试图在simplecart中的项目上添加错误参数时获得警报。 当字段为空或如果其值低于400或高于500时 一切正常,但字段为空时警报不起作用 Html: <input required max="500" min="400" type="number" class="item_width"> <script> simpleCart.bind( 'beforeAdd' , function( item ){ if( item.get( 'width' ) <
<input required max="500" min="400" type="number" class="item_width">
<script>
simpleCart.bind( 'beforeAdd' , function( item ){
if( item.get( 'width' ) < '400'
|| item.get( 'width' ) > '500'
|| item.get( 'width' ) === 'null')
{
alert("Choose between 400 and 500");
return false;
}
});
</script>
JavaScript:
<input required max="500" min="400" type="number" class="item_width">
<script>
simpleCart.bind( 'beforeAdd' , function( item ){
if( item.get( 'width' ) < '400'
|| item.get( 'width' ) > '500'
|| item.get( 'width' ) === 'null')
{
alert("Choose between 400 and 500");
return false;
}
});
</script>
simpleCart.bind('beforeAdd',函数(项){
如果(item.get('width')<'400'
||item.get('width')>'500'
||item.get('width')='null')
{
警报(“在400和500之间选择”);
返回false;
}
});
我不知道simplecart如何处理空字符串,但您编写了'null'
,这意味着输入的文本必须是“null”
,一个字符串,而不是对象null
。此外,空字符串不是null,而是空字符串,请尝试:
simpleCart.bind( 'beforeAdd' , function( item ){
if ( item.get( 'width' ) < '400'
|| item.get( 'width' ) > '500'
|| item.get( 'width' ) == null
|| item.get( 'width' ) === '')
{
alert("Choose between 400 and 500");
return false;
}
});
要确保item.get('width')实际为您提供了一个整数,您应该解析它们:
...
parseInt( item.get('width') ) < ...
...
。。。
parseInt(item.get('width'))<。。。
...
试试这个
if( item.get( 'width' ) < '400' || item.get( 'width' ) > '500' || item.get( 'width' ) === 'null' || item.get( 'width' ) === '')
{ alert("Choose between 400 and 500"); }
if(item.get('width')<'400'| | | item.get('width')>'500'| | | item.get('width')=='null'| | item.get('width')=='
{alert(“在400和500之间选择”);}
您应该设置一个var item\u width=item.get('width')在if语句中使用,而不是重复项。为每次比较获取('width')
。