为什么isNaN第二次被调用时不工作?

Vin*_*nce 2 html javascript jquery twitter-bootstrap

我需要验证非数字字符的各种输入字段.我也在使用Bootstrap警报.所以我有下面的代码.第一次出错时它工作正常.你得到BS警报.但是,如果你在任何后续输入中输入错误代码不起作用,即我没有得到警报.我检查了两个vars数量和数量的内容,实际上在第二次传递代码时,它们确实包含了错误非数字字符.但由于某种原因,NaN没有看到它.我究竟做错了什么?

$(".product_table").on('change', '.edit_quantity',function (){

  var qty = $(this).find('input').val();
  var quantity = $(this).parents(':eq(1)').find('input').filter(".edit_quantity").val();

  console.log('l 117', qty);
  console.log('l 118', quantity);

  if (isNaN(quantity||qty))
  {
      $("#char_error").show();
  }
  else
  {
  //more code goes here//
  }
}
Run Code Online (Sandbox Code Playgroud)

HTML:

 <div id="char_error" class="alert alert-danger">
    <strong>Error</strong> You have entered an illegal character in the quantity box, please enter a number.
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
</div>
Run Code Online (Sandbox Code Playgroud)

此外,我通过隐藏启动我的JS文件.<div id="char_error">这也是不理想的内容闪烁进入视图然后消失.

Qwe*_*tiy 5

因为它只被调用一次.

if (isNaN(quantity||qty)) {
Run Code Online (Sandbox Code Playgroud)

是一样的

var t = quantity||qty;
if (isNaN(t)) {
Run Code Online (Sandbox Code Playgroud)

而不是你需要的

if (isNaN(quantity) || isNaN(qty)) {
Run Code Online (Sandbox Code Playgroud)