cur*_*ets 0 javascript jquery if-statement
如何查看SPAN是否包含超过3位数的数字?我尝试了以下,但它没有奏效.
if ($('.pointScore .score').val() > 999 ) {
alert("Over 999 point");
}
Run Code Online (Sandbox Code Playgroud)
先感谢您.
您注意到a span没有值,但代码存在另一个潜在问题.文本是一个字符串,因此您将字符串与数字进行比较.由于这可能会产生意想不到的结果,因此应该避免.
最简单的方法是简单地检查字符串的长度:
if ($('.pointScore .score').text().length > 3) {
alert("Over 999 point");
}
Run Code Online (Sandbox Code Playgroud)
您还可以将字符串解析为数字:
if (parseInt($('.pointScore .score').text(), 10) > 999) {
alert("Over 999 point");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3927 次 |
| 最近记录: |