应该为真的条件是假的(JS)
Condition that should be true is false (JS)
我有一些 JS 和 HTML 代码。当用户点击提交按钮时,我想知道哪个数字输入框 (input type="number"/>
) 有文本。我的条件语句是:document.getElementById("fallen").value != 0 || document.getElementById("fallen").value != ""
。由于某种原因,这不起作用。我尝试使用 document.getElementById("fallen").value == null
.
感谢任何帮助,提前致谢!
任何输入的值总是一个字符串。该语句按预期工作,但您可以进行一项调整以使其根据您的要求工作:
parseInt(document.getElementById('fallen').value, 10) !== 0
要么
document.getElementById('fallen').value !== '0'
类似的东西?
const inputs = document.querySelectorAll('input[type=number]')
document.querySelector('input[type=submit]').addEventListener('click', () =>{
inputs.forEach( o => {
if( o.value ) console.log(o.getAttribute('name'))
})
})
使用正则表达式检查字段,例如:
If (/^[0-9]{1,}$/.test(myvalue)) console.log('is a number')
否则 console.log('is not numerical')
注意:正则表达式测试函数在 myvalue 等于 null 时也有效。
看到那个代码,我想这就是你需要的
function compare(){
var input =$('input[type="number"]')
if(!input.val() || input.val()==0){
console.log ('empty');
}
else{
console.log('the content is '+input.val());
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='number' >
<button onclick=compare()>check</button>
我有一些 JS 和 HTML 代码。当用户点击提交按钮时,我想知道哪个数字输入框 (input type="number"/>
) 有文本。我的条件语句是:document.getElementById("fallen").value != 0 || document.getElementById("fallen").value != ""
。由于某种原因,这不起作用。我尝试使用 document.getElementById("fallen").value == null
.
感谢任何帮助,提前致谢!
任何输入的值总是一个字符串。该语句按预期工作,但您可以进行一项调整以使其根据您的要求工作:
parseInt(document.getElementById('fallen').value, 10) !== 0
要么
document.getElementById('fallen').value !== '0'
类似的东西?
const inputs = document.querySelectorAll('input[type=number]')
document.querySelector('input[type=submit]').addEventListener('click', () =>{
inputs.forEach( o => {
if( o.value ) console.log(o.getAttribute('name'))
})
})
使用正则表达式检查字段,例如:
If (/^[0-9]{1,}$/.test(myvalue)) console.log('is a number') 否则 console.log('is not numerical')
注意:正则表达式测试函数在 myvalue 等于 null 时也有效。
看到那个代码,我想这就是你需要的
function compare(){
var input =$('input[type="number"]')
if(!input.val() || input.val()==0){
console.log ('empty');
}
else{
console.log('the content is '+input.val());
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='number' >
<button onclick=compare()>check</button>