javascript 如果输入大于0,则无法获取数值输入的值

mepcadol  于 2023-02-15  发布在  Java
关注(0)|答案(1)|浏览(107)
<input type="number" placeholder="Number of people*" class="user_number" min="1" max="5">
let user_number = document.getElementsByClassName("user_number");
if($(user_number).val() > 0){
            console.log($(user_number).val());
        }

但当我把任何超过0控制台不显示帮助请

luaexgnf

luaexgnf1#

您可以为"input"事件附加一个事件侦听器,以便在每次更改值时检查该值。

$('.user_number').on('input', function() {
  if (this.valueAsNumber > 0) {
    console.log(this.value);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" placeholder="Number of people*" class="user_number" min="1" max="5">

相关问题