在简单购物车中添加项目时,输入字段"type=number"为空时发出警报



我试图在simplecart中添加错误参数时获得警报。当字段为空或低于400或高于500时。

一切正常,但当字段为空时不发出警告。

Html:

<input required max="500" min="400" type="number"  class="item_width">
JavaScript:

<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如何处理空字符串,但是您写了'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; 
}
});

同样,通过<>string测试整数也不是好的编程。它可能会工作,但请正确编码:

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; 
}
});

确保项目。Get ('width')实际上给你一个整数,你应该解析它们:

...
parseInt( item.get('width') ) < ...
...

try this,

 if( item.get( 'width' ) < '400' || item.get( 'width' ) > '500' || item.get( 'width' ) === 'null' || item.get( 'width' ) === '')
  {   alert("Choose between 400 and 500");   }

最新更新