At first I was doing this, but then I realized this can't work because not all the elements will be visible. I don't mean hidden, I mean it won't even show up in the DOM. This whole chunk of code (minus the document.ready part) is done 6 times for 6 fields that have 6 different names, so when either of the 6 are typed into it checks the other 5.
$(document).ready(function() {
$(document.body).on("input", "input[name='count1']", function() {
if ($(this).val() == '') {
//do nothing
} else {
var count1val = $("#count1").val();
var count2val = $("#count2").val();
var count3val = $("#count3").val();
var count4val = $("#count4").val();
var count5val = $("#count5").val();
var count6val = $("#count6").val();
if (count1val !== '' && count2val !== '' && count3val !== '' && count4val !== '' && count5val !== '' && count6val !== '') {
//do something
}
}
});
});
So then I needed to see if all inputs that are visible with a certain class name are empty. Needs to be with class name because there will be multiple sets I need to handle the same way and I don't want one set to interfere with other sets. I tried the following but it didn't work.
$(document).ready(function() {
$(document.body).on("input", "input[name='count1']", function() {
if ($(this).val() == '') {
//do nothing
} else {
if ($(".countmulti:visible").val() !== '') {
//do something
}
}
});
});
What is a better way to find out if all of a set of visible fields with a specific class name are empty or not.
To explain better about the visible fields; sometimes it will be only field 1 visible, sometimes field 1 and 2, sometimes 1, 2, and 3, sometimes 1, 2, 3, and 4, sometimes 1, 2, 3, 4, and 5, and sometimes all 6.
i solved it with another Q&A on stackoverflow. here is what i used:
var inputsWithNoValue = $(".countmulti:visible").filter(function() {
return !this.value.length;
});
if (inputsWithNoValue.length) { // if there are inputs with no value
alert('something empty');
} else {
alert('nothing empty');
}
Aucun commentaire:
Enregistrer un commentaire