Function adds an element to list every time someone opens a form
-
08-02-2021 - |
Вопрос
I have a form in which a user chooses "Standard", "CAD" or "Manager" which will add an element to a list automatically.
The function onChangeMassnahmen()
will check if the checkbox is checked:
function onChangeMassnahmen(){
var curvalue = NWF$("#" + varMassnahmen).find("input:checked").val();
if (curvalue == "Ja") {
NWF$(".cssMassnahmenType").hide();
} else if (curvalue === "") {
NWF$(".cssMassnahmenType").hide();
} else{
NWF$(".cssMassnahmenType").show();
}
}
In my onLoadForm()
which starts after a NWF$(document).ready(function()
I have the following codepart:
NWF$("#" + varCataloguePicker).change(onChangeEquipment);
onChangeEquipment();
The onChangeEquipment()
function will add an element to a list depending on whether "Standard", "CAD" or "Manager" has been checked :
function onChangeEquipment(){
var curvalue = NWF$("#" + varCataloguePicker).find("input:checked").val();
if (curvalue == "Standard"){
fillStandard1();
fillStandard2();
} else if (curvalue == "CAD"){
fillCAD();
} else if (curvalue == "Manager"){
fillManager();
} else {
console.log("Else");
}
}
The problem I have is that since onChanceEquipment()
is in the onLoadForm()
it will add an element to a list every time the form opens. I want it to add an element to a list once, and not every time the form opens. It's not a bug, it's a logical mistake, but I don't know how to solve it. Tried working with booleans, but the values reset after a page refresh. Does someone know how one can solve this problem?
Решение
You will find that pretty much all bugs are logical mistakes. :)
You say you have the following lines of code in your onFormLoad()
function, which runs when the the page loads:
// this line attaches your onChangeEquipment function
// as the change handler for the radio buttons control
NWF$("#" + varCataloguePicker).change(onChangeEquipment);
// this line immediately invokes that function,
// as if someone just changed the value of the radio buttons
onChangeEquipment();
Why do you have that second line? Always invoking a function that adds an element to a list inside a function that runs on every page load will lead to exactly what you are seeing - that an element gets added to a list on every page load.