当输入框存在''和0时,要区分是输入的0还是没有输入过
image.png
Boolean数字0为false,字符0为true。
只需要 Boolean(value+'')就可以区分了。
ps:同理还有??操作符号,有时可以省事。
undefined??2 //2 只有左侧为undefined时才会显示右边
0??2 // 0
''??2 // 0
当输入框存在''和0时,要区分是输入的0还是没有输入过
image.png
Boolean数字0为false,字符0为true。
只需要 Boolean(value+'')就可以区分了。
ps:同理还有??操作符号,有时可以省事。
undefined??2 //2 只有左侧为undefined时才会显示右边
0??2 // 0
''??2 // 0
本文标题:js 最简单判断是否有值
本文链接:https://www.haomeiwen.com/subject/brfysrtx.html
网友评论