您仅检查.hoogtebreedte
类的第一个值,因此第二个值未被检查。
下面是工作示例:
function test () {
if($('.hoogtebreedte').eq(0).val() !== '' && $('.hoogtebreedte').eq(0).val() > 0 && $('.hoogtebreedte').eq(1).val() !== '' && $('.hoogtebreedte').eq(1).val() > 0){
console.log('everything is ok')
} else {
console.log('something is wrong')
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" class="formateninput form-control hoogtebreedte" name="breedte" max="99" min="0">
<input type="number" class="formateninput form-control hoogtebreedte" name="hoogte" max="99" min="0">
<button onclick='test()'>test</button>
此外,对于多个课程,您可以使用以下代码:
function validate () {
const validate = Array.from($('.hoogtebreedte')).every((_,index) => $('.hoogtebreedte').eq(index).val() !== '' && $('.hoogtebreedte').eq(index).val() > 0)
return validate ? console.log('everything is ok') : console.log('something is wrong')
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" class="formateninput form-control hoogtebreedte" name="breedte" max="99" min="0">
<input type="number" class="formateninput form-control hoogtebreedte" name="hoogte" max="99" min="0">
<input type="number" class="formateninput form-control hoogtebreedte" name="hoogte" max="99" min="0">
<button onclick='validate()'>validate</button>
,
由于输入元素的值始终是字符串,因此在进行验证之前,您必须将输入的值解析为整数。
工作示例:
const check = () => {
let a = parseInt(document.getElementById("a").value)
let b = parseInt(document.getElementById("b").value)
if(a >= 1 && b >= 2) {
alert("Everything is good!")
} else {
alert("Either 'a' or 'b' are less than 1 or empty.")
}
}
<input type="number" id="a">
<input type="number" id="b">
<button id="check" onclick="check()">Check</button>
注意:我不检查等于空字符串的值,因为parseFloat对于无法解析为整数的值将返回NaN(非数字),并且NaN> = 1始终为假。
,
<input type="number" id="breedte" class="formateninput form-control hoogtebreedte" name="breedte">
<input type="number" id="hoogte" class="formateninput form-control hoogtebreedte" name="hoogte">
<input id="btnSubmit" type="button" value="Submit" />
$(document).ready(function () {
$('#btnSubmit').click(function () {
var input1 = parseInt($('#breedte').val());
var input2 = parseInt($('#hoogte').val());
if (input1 > 0 && input2 > 0) {
alert('Go ahead');
} else {
alert('Stop!!!');
}
});
});
,
这只是因为您正在从jQuery中的class
获取价值而将其视为数组。
因此从逻辑上来说,您是第二次输入未得到验证。
each
将解决您的问题。
$('.validate').on('click',function(){
var status = false;
$('.hoogtebreedte').each(function(){
var t = $(this);
if(t.val() === '' || t.val() == 0 || t.val() < 0){
status = true
}
})
if(status) {
console.log('stop')
} else {
console.log('go')
}
})
Working Code here
,
添加Select st.name as staff_member,Sp.name as supervisor
From staff st
Inner join staff sp
On (st.supervisor = sp.snum)
Where st.city <> sp.city;
事件,以便在每次引入数字时检查条件,并使函数“通用”可在任何输入上使用。如果插入的值为0或1,请输入红色
on('input')
function validateInput(myInput) {
$('#' + myInput).on('input',function(evt) {
var value = evt.target.value
console.log(value)
if (value === '0' || value === '1') {
evt.target.className = 'invalid'
} else {
evt.target.className = 'valid'
}
})
}
validateInput('first')
validateInput('second')
input.valid {
border: 1px solid green;
}
input.invalid {
border: 1px solid red;
}
本文链接:https://www.f2er.com/3159406.html