【发布时间】:2015-09-16 22:45:38
【问题描述】:
我目前正在使用 jQuery 检查 value 是否为 Nan,并希望将检查扩展到非 null 或空,但不确定如何执行此操作, 这是我的代码;
<script type="text/javascript"> // ensure quantity textbox is numeric
$(document).ready(function () {
$('[id$=txtQuantity]').change(function () {
if(isNaN(this.value)) {
alert("Please ensure the quantity specified is numeric");
$(this).val("1");
}
else{
$(this).val(this.value);
}
});
});
【问题讨论】:
-
将
if(isNaN(this.value)) {更改为if(!this.value || isNaN(this.value)) { -
如果认为他可能想要
if(!this.value || isNaN(this.value)),换句话说,如果它为 null 或 NAN -
@BenRobinson 是的,已经编辑过了。 :-)