英文:
Can't get value of numeric input if input is greater then 0
问题
<input type="number" placeholder="人数*" class="user_number" min="1" max="5">
let user_number = document.getElementsByClassName("user_number");
if ($(user_number).val() > 0) {
console.log($(user_number).val());
}
英文:
<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());
}
but when i put anything more than 0 console doesn't show up help please
答案1
得分: 1
你可以为"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="人数*" class="user_number" min="1" max="5">
英文:
You can attach an event listener for the "input" event so that the value is checked each time it changes.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$('.user_number').on('input', function() {
if (this.valueAsNumber > 0) {
console.log(this.value);
}
});
<!-- language: lang-html -->
<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">
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论