无法在 JS 中删除动态选择字段
Can't delete a dynamic selection field in JS
我在删除动态创建的对象选择字段时遇到问题。我已经尝试了几种方法来删除字段,包括在创建对象时将它们放入数组中,然后尝试将它们弹出。基本上我想要做的是能够根据需要添加选择字段,并使用选择字段旁边的十字按钮将它们 1 个 1 个删除。
代码不是我的,我只是从 JS、HTML 和 CSS 开始。我更像是一个 python 人。
所以有人可以帮我完成它吗?我正在为一家公司做这件事,所以失败是一个很大的禁忌
附上代码图片和结果:
问候
El_Capit4n
PS.: 不要理会 flow_variables 错误...我在代码中禁用了它们以使其成为 运行.
开头是动态字段创建的声明:
My Code
Its output
请注意,它只是程序的一部分
整个JS/HTML/CSS代码:
// Global vars
var body = document.getElementsByTagName('body')[0];
var feature_options = ["MIN","MAX","MEAN"];
var operator_options = [">","<","=",">=","<=","LIKE"];
var i;
const arrayselection = [];
var x;
var criterium_num=1;
var criteria_count=0;
var first_key_flag=true;
/* ********** HTML ********** */
html ='<h1> Filterentwurf </h1>';
html+='<p id="debug">Debug</p>';
html += '<form id="criteria_form" autocomplete="off" action="/action_page.php"></form>';
html += '<br><button class="buttons" id="delete"> X </button><br>';
html += '<br><button class="buttons" id="btn"> <b>+</b> Neues Kriterium hinzufügen</button><br>';
filter_conf= //'<form action="/action_page.php" class="filter_conf" id="filter_class">'+
'<p>Filter Einstellung:</p>'+
'<input type="radio" id="no_filt" name="filter_conf" value="4">'+
'<label for="no_filt">Keine Signale auswählen</label><br>'+
'<input type="radio" id="filt_conjunction" name="filter_conf" value="1">'+
'<label for="filt_conjunction">Alle Kriterien erfüllen</label><br>'+
'<input type="radio" id="filt_disjunction" name="filter_conf" value="2">'+
'<label for="filt_disjunction">Mindestens ein Kriterium erfüllen</label><br>'+
'<input type="radio" id="filt_advanced" name="filter_conf" value="3">'+
'<label for="filt_advanced">Manuele Eingabe der Filtereinstellung</label><br>',
//'</form>';
html +=filter_conf;
html += '<input type="text" id="filter_def" style="display:none">';
body.innerHTML=html;
/* ********** JavaScript ********** */
// Add new criteria by clucking on button
document.querySelector("#btn").onclick = function add_signal(event) {
x= create_filter_criterium(criterium_num);
arrayselection.push(x)
criterium_num+=1;
}
document.getElementById("delete").onclick = function(event) {
const index = arrayselection.indexOf(criterium_num);
arrayselection.splice(index, 1);
criterium_num-= 1;
}
// Add function to hide and show ComplexQuery
document.querySelector("#filt_advanced").onclick = function(event) {
document.getElementById("filter_def").style.display="block"
FLOW_VARIABLES["filter_def"]="filt_advanced";
}
document.querySelector("#no_filt").onclick = function(event){
document.getElementById("filter_def").style.display="none"
document.getElementById("filter_def").value=""
FLOW_VARIABLES["filter_def"]="No_Signal";
}
document.querySelector("#filt_conjunction").onclick = function(event) {
document.getElementById("filter_def").style.display="none"
document.getElementById("filter_def").value=""
FLOW_VARIABLES["filter_def"]="conjunction";
}
document.querySelector("#filt_disjunction").onclick = function(event) {
document.getElementById("filter_def").style.display="none"
document.getElementById("filter_def").value=""
FLOW_VARIABLES["filter_def"]="disjunction";
}
// inititialize input field for first criterium
document.addEventListener("load", function(event) {
// somehow not working ...
document.getElementById("debug").innerHTML = "doc loaded";
create_filter_criterium(1);
criterium_num+=1;
});
document.addEventListener("onmousemove", function(event) {
// substitute of document.addEventListener("load", function(event) ... somehow not working
//event activated after first keypressed
if (first_key_flag=true){
document.getElementById("debug").innerHTML = "doc loaded";
create_filter_criterium(1);
criterium_num+=1;
}
first_key_flag=false;
});
// create new criterium
function create_filter_criterium(criterium_num){
document.getElementById("debug").innerHTML = "Button Presed";
criteria=document.getElementById("criteria_form");
//Signal
signal_input=document.createElement("input");
signal_input.id = "signal"+criterium_num.toString();
signal_input.type = "text";
signal_input.placeholder="Signal "+criterium_num.toString();
signal_div=document.createElement("div");
signal_div.className="autocomplete";
signal_div.style="width:50%";
signal_div.innerHTML=signal_input.outerHTML;
// feature
feature_input=document.createElement("input");
feature_input.id = "feature"+criterium_num.toString();
feature_input.type = "text";
feature_input.placeholder="Merkmal "+criterium_num.toString();
feature_div=document.createElement("div");
feature_div.className="autocomplete";
feature_div.style="width:10%";
feature_div.innerHTML=feature_input.outerHTML;
// operator
operator_input=document.createElement("input");
operator_input.id = "operator"+criterium_num.toString();
operator_input.type = "text";
operator_input.placeholder="Operator "+criterium_num.toString();
operator_div=document.createElement("div");
operator_div.className="autocomplete";
operator_div.style="width:10%";
operator_div.innerHTML=operator_input.outerHTML;
// value
value_input=document.createElement("input");
value_input.id = "value"+criterium_num.toString();
value_input.type = "text";
value_input.placeholder="Wert "+criterium_num.toString();
value_div=document.createElement("div");
value_div.className="autocomplete";
value_div.style="width:15%";
value_div.innerHTML=value_input.outerHTML;
// index
index_input=document.createElement("P");
index_input.id = "index"+criterium_num.toString();
index_input.innerText = "Q"+criterium_num.toString();
//index_input.placeholder="Wert "+criterium_num.toString();
index_div=document.createElement("div");
index_div.className="autocomplete";
index_div.style="width:5%";
index_div.innerHTML=index_input.outerHTML;
// white space
white_space=document.createElement("P");
white_space.innerText = "";
white_space_div=document.createElement("div");
white_space_div.className="autocomplete";
white_space_div.style="width:2%";
white_space_div.innerHTML=white_space.outerHTML;
ws=white_space_div.outerHTML;
var criterium=document.createElement("div");
criterium.display="block";
criterium.innerHTML=index_div.outerHTML+ws+signal_div.outerHTML+ws+feature_div.outerHTML+ws+operator_div.outerHTML+ws+value_div.outerHTML;
criteria.appendChild(criterium);
criteria_count+=1;
autocomplete(document.getElementById("signal"+criterium_num.toString()), signal_options);
dropdown(document.getElementById("feature"+criterium_num.toString()), feature_options);
dropdown(document.getElementById("operator"+criterium_num.toString()), operator_options);
}
// autocomplete functions
function autocomplete(inp, arr) {
/*the autocomplete function takes two arguments,
the text field element and an array of possible autocompleted values:*/
var currentFocus;
/*execute a function when someone writes in the text field:*/
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
/*close any already open lists of autocompleted values*/
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
/*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
/*append the DIV element as a child of the autocomplete container:*/
this.parentNode.appendChild(a);
/*for each item in the array...*/
for (i = 0; i < arr.length; i++) {
/*check if the item starts with the same letters as the text field value:*/
//if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
if (arr[i].toUpperCase().indexOf(val.toUpperCase())!== -1) {
/*create a DIV element for each matching element:*/
b = document.createElement("DIV");
/*make the matching letters bold:*/
//b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
//b.innerHTML += arr[i].substr(val.length);
b.innerHTML = arr[i].substr(0, arr[i].toUpperCase().indexOf(val.toUpperCase()));
b.innerHTML += "<strong>" + arr[i].substr(arr[i].toUpperCase().indexOf(val.toUpperCase()), val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length+arr[i].toUpperCase().indexOf(val.toUpperCase()))
//b.innerHTML += arr[i]; // not bold display
/*insert a input field that will hold the current array item's value:*/
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
/*execute a function when someone clicks on the item value (DIV element):*/
b.addEventListener("click", function(e) {
/*insert the value for the autocomplete text field:*/
inp.value = this.getElementsByTagName("input")[0].value;
/*close the list of autocompleted values,
(or any other open lists of autocompleted values:*/
closeAllLists();
});
a.appendChild(b);
}
}
});
/*execute a function presses a key on the keyboard:*/
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
/*If the arrow DOWN key is pressed,
increase the currentFocus variable:*/
currentFocus++;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 38) { //up
/*If the arrow UP key is pressed,
decrease the currentFocus variable:*/
currentFocus--;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 13) {
/*If the ENTER key is pressed, prevent the form from being submitted,*/
e.preventDefault();
if (currentFocus > -1) {
/*and simulate a click on the "active" item:*/
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
/*a function to classify an item as "active":*/
if (!x) return false;
/*start by removing the "active" class on all items:*/
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
/*add class "autocomplete-active":*/
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
/*a function to remove the "active" class from all autocomplete items:*/
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
/*close all autocomplete lists in the document,
except the one passed as an argument:*/
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
/*execute a function when someone clicks in the document:*/
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
// dropdown functions
function dropdown(inp, arr) {
/*the dropdown function takes two arguments,
the text field element and an array of possible autocompleted values:*/
var currentFocus;
/*execute a function when someone writes in the text field:*/
inp.addEventListener("mouseover", function(e) {
var a, b, i, val = this.value;
/*close any already open lists of autocompleted values*/
closeAllLists();
//if (!val) { return false;}
currentFocus = -1;
/*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
/*append the DIV element as a child of the autocomplete container:*/
this.parentNode.appendChild(a);
/*for each item in the array...*/
for (i = 0; i < arr.length; i++) {
/*check if the item starts with the same letters as the text field value:*/
//if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
/*create a DIV element for each matching element:*/
b = document.createElement("DIV");
b.innerHTML = arr[i];
/*insert a input field that will hold the current array item's value:*/
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
/*execute a function when someone clicks on the item value (DIV element):*/
b.addEventListener("click", function(e) {
/*insert the value for the autocomplete text field:*/
inp.value = this.getElementsByTagName("input")[0].value;
/*close the list of autocompleted values,
(or any other open lists of autocompleted values:*/
closeAllLists();
});
a.appendChild(b);
}
/*close dropdown after moving the pointer out of it*/
a.addEventListener("mouseleave", function(e) {
closeAllLists();
});
});
/*execute a function presses a key on the keyboard:*/
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
/*If the arrow DOWN key is pressed,
increase the currentFocus variable:*/
currentFocus++;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 38) { //up
/*If the arrow UP key is pressed,
decrease the currentFocus variable:*/
currentFocus--;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 13) {
/*If the ENTER key is pressed, prevent the form from being submitted,*/
e.preventDefault();
if (currentFocus > -1) {
/*and simulate a click on the "active" item:*/
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
/*a function to classify an item as "active":*/
if (!x) return false;
/*start by removing the "active" class on all items:*/
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
/*add class "autocomplete-active":*/
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
/*a function to remove the "active" class from all autocomplete items:*/
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
/*close all autocomplete lists in the document,
except the one passed as an argument:*/
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
/*execute a function when someone clicks in the document:*/
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
// Set Output variables as a ','-concatenated string of the manually selected signals
setInterval(function(){
FLOW_VARIABLES["debug"]=6;
//document.getElementById("debug").innerHTML = "Criteria Count: "+criteria_count.toString();
document.getElementById("debug").innerHTML = "";
/* actualize input variables every 0.2 seconds*/
FLOW_VARIABLES["signals"]='';
FLOW_VARIABLES["features"]='';
FLOW_VARIABLES["operators"]='';
FLOW_VARIABLES["values"]='';
FLOW_VARIABLES["indices"]='';
var i;
for (i = 1; i <= criteria_count; i++) {
FLOW_VARIABLES["signals"]+=document.getElementById("signal"+i.toString()).value;
FLOW_VARIABLES["features"]+=document.getElementById("feature"+i.toString()).value;
FLOW_VARIABLES["operators"]+=document.getElementById("operator"+i.toString()).value;
FLOW_VARIABLES["values"]+=document.getElementById("value"+i.toString()).value;
FLOW_VARIABLES["indices"]+=document.getElementById("index"+i.toString()).innerText;
if (i!=criteria_count){
FLOW_VARIABLES["signals"]+=',';
FLOW_VARIABLES["features"]+=',';
FLOW_VARIABLES["operators"]+=',';
FLOW_VARIABLES["values"]+=',';
FLOW_VARIABLES["indices"]+=',';
}
}
//define filter as user's input
if (document.getElementById("filt_advanced").value){
FLOW_VARIABLES["filter_def"]=document.getElementById("filter_def").value;
}
}, 100);
body {
font-family: sans-serif;
}
h1 {
font-size: 1.1em;
font-weight: bold;
}
.success {
color: #006633;
font-weight: bold;
}
.background {
color: #000000;
}
.filter_conf{
color: #ffffff;
display: block;
}
.buttons {
margin: 3px 8px;
border: 1px solid transparent;
padding: 7px;
font-size: 16px;
}
.failure {
color: #990000;
font-weight: bold;
}
* { box-sizing: border-box; }
body {
font: 16px Arial;
}
.autocomplete {
/*the container must be positioned relative:*/
position: relative;
display: inline-block;
}
input {
margin: 3px 8px;
border: 1px solid transparent;
background-color: #f1f1f1;
padding: 7px;
font-size: 16px;
}
input[type=radial] {
width: 3%;
}
input[type=text] {
background-color: #f1f1f1;
width: 100%;
}
input[type=submit] {
background-color: DodgerBlue;
color: #fff;
}
.autocomplete-items {
position: absolute;
border: 21px solid #d4d4d4;
border-bottom: none;
border-top: none;
z-index: 99;
/*position the autocomplete items to be the same width as the container:*/
top: 100%;
left: 0;
right: 0;
}
.autocomplete-items div {
padding: 10px;
cursor: pointer;
background-color: #fff;
border-bottom: 1px solid #d4d4d4;
}
.autocomplete-items div:hover {
/*when hovering an item:*/
background-color: #e9e9e9;
}
.autocomplete-active {
/*when navigating through the items using the arrow keys:*/
background-color: DodgerBlue !important;
color: #ffffff;
}
我找到了错误。您必须 increment/decrement criterium_num 在 onclick 事件中从 2 个按钮开始。要删除条件,您必须搜索相应的 div 并将其 outerHTML 设置为 ''。为了防止获得标准的负面索引,我将其设置为 1,如果它更小 1 通过多次单击 delte 按钮(删除太多)。
document.querySelector("#btn").onclick = function add_signal(event) {
if (criterium_num<1) criterium_num=1;
x = create_filter_criterium(criterium_num++);
arrayselection.push(x)
}
document.getElementById("delete").onclick = function(event) {
const index = arrayselection.indexOf(criterium_num--);
document.getElementById('criterium_'+criterium_num).outerHTML='';
arrayselection.splice(index, 1);
}
要找到这个 div,您需要在条件中添加一个 ID。
var criterium=document.createElement("div");
criterium.display="block";
criterium.id='criterium_'+criterium_num;
criterium.innerHTML=index_div.outerHTML+ws+signal_div.outerHTML+ws+feature_div.outerHTML+ws+operator_div.outerHTML+ws+value_div.outerHTML;
为了测试,您可以使用 https://jsfiddle.net/r2ya49cv/1/ 完整代码在哪里,而这里只列出了相关行。
请控制视图和标准中的索引是否正确,因为您使用的是生产系统。
备注:下次请缩短您的代码,仅针对相关部分。其他人搜索相关部分很繁琐,这使得它变得更加复杂。
我在删除动态创建的对象选择字段时遇到问题。我已经尝试了几种方法来删除字段,包括在创建对象时将它们放入数组中,然后尝试将它们弹出。基本上我想要做的是能够根据需要添加选择字段,并使用选择字段旁边的十字按钮将它们 1 个 1 个删除。
代码不是我的,我只是从 JS、HTML 和 CSS 开始。我更像是一个 python 人。
所以有人可以帮我完成它吗?我正在为一家公司做这件事,所以失败是一个很大的禁忌
附上代码图片和结果:
问候 El_Capit4n
PS.: 不要理会 flow_variables 错误...我在代码中禁用了它们以使其成为 运行.
开头是动态字段创建的声明:
My Code Its output
请注意,它只是程序的一部分
整个JS/HTML/CSS代码:
// Global vars
var body = document.getElementsByTagName('body')[0];
var feature_options = ["MIN","MAX","MEAN"];
var operator_options = [">","<","=",">=","<=","LIKE"];
var i;
const arrayselection = [];
var x;
var criterium_num=1;
var criteria_count=0;
var first_key_flag=true;
/* ********** HTML ********** */
html ='<h1> Filterentwurf </h1>';
html+='<p id="debug">Debug</p>';
html += '<form id="criteria_form" autocomplete="off" action="/action_page.php"></form>';
html += '<br><button class="buttons" id="delete"> X </button><br>';
html += '<br><button class="buttons" id="btn"> <b>+</b> Neues Kriterium hinzufügen</button><br>';
filter_conf= //'<form action="/action_page.php" class="filter_conf" id="filter_class">'+
'<p>Filter Einstellung:</p>'+
'<input type="radio" id="no_filt" name="filter_conf" value="4">'+
'<label for="no_filt">Keine Signale auswählen</label><br>'+
'<input type="radio" id="filt_conjunction" name="filter_conf" value="1">'+
'<label for="filt_conjunction">Alle Kriterien erfüllen</label><br>'+
'<input type="radio" id="filt_disjunction" name="filter_conf" value="2">'+
'<label for="filt_disjunction">Mindestens ein Kriterium erfüllen</label><br>'+
'<input type="radio" id="filt_advanced" name="filter_conf" value="3">'+
'<label for="filt_advanced">Manuele Eingabe der Filtereinstellung</label><br>',
//'</form>';
html +=filter_conf;
html += '<input type="text" id="filter_def" style="display:none">';
body.innerHTML=html;
/* ********** JavaScript ********** */
// Add new criteria by clucking on button
document.querySelector("#btn").onclick = function add_signal(event) {
x= create_filter_criterium(criterium_num);
arrayselection.push(x)
criterium_num+=1;
}
document.getElementById("delete").onclick = function(event) {
const index = arrayselection.indexOf(criterium_num);
arrayselection.splice(index, 1);
criterium_num-= 1;
}
// Add function to hide and show ComplexQuery
document.querySelector("#filt_advanced").onclick = function(event) {
document.getElementById("filter_def").style.display="block"
FLOW_VARIABLES["filter_def"]="filt_advanced";
}
document.querySelector("#no_filt").onclick = function(event){
document.getElementById("filter_def").style.display="none"
document.getElementById("filter_def").value=""
FLOW_VARIABLES["filter_def"]="No_Signal";
}
document.querySelector("#filt_conjunction").onclick = function(event) {
document.getElementById("filter_def").style.display="none"
document.getElementById("filter_def").value=""
FLOW_VARIABLES["filter_def"]="conjunction";
}
document.querySelector("#filt_disjunction").onclick = function(event) {
document.getElementById("filter_def").style.display="none"
document.getElementById("filter_def").value=""
FLOW_VARIABLES["filter_def"]="disjunction";
}
// inititialize input field for first criterium
document.addEventListener("load", function(event) {
// somehow not working ...
document.getElementById("debug").innerHTML = "doc loaded";
create_filter_criterium(1);
criterium_num+=1;
});
document.addEventListener("onmousemove", function(event) {
// substitute of document.addEventListener("load", function(event) ... somehow not working
//event activated after first keypressed
if (first_key_flag=true){
document.getElementById("debug").innerHTML = "doc loaded";
create_filter_criterium(1);
criterium_num+=1;
}
first_key_flag=false;
});
// create new criterium
function create_filter_criterium(criterium_num){
document.getElementById("debug").innerHTML = "Button Presed";
criteria=document.getElementById("criteria_form");
//Signal
signal_input=document.createElement("input");
signal_input.id = "signal"+criterium_num.toString();
signal_input.type = "text";
signal_input.placeholder="Signal "+criterium_num.toString();
signal_div=document.createElement("div");
signal_div.className="autocomplete";
signal_div.style="width:50%";
signal_div.innerHTML=signal_input.outerHTML;
// feature
feature_input=document.createElement("input");
feature_input.id = "feature"+criterium_num.toString();
feature_input.type = "text";
feature_input.placeholder="Merkmal "+criterium_num.toString();
feature_div=document.createElement("div");
feature_div.className="autocomplete";
feature_div.style="width:10%";
feature_div.innerHTML=feature_input.outerHTML;
// operator
operator_input=document.createElement("input");
operator_input.id = "operator"+criterium_num.toString();
operator_input.type = "text";
operator_input.placeholder="Operator "+criterium_num.toString();
operator_div=document.createElement("div");
operator_div.className="autocomplete";
operator_div.style="width:10%";
operator_div.innerHTML=operator_input.outerHTML;
// value
value_input=document.createElement("input");
value_input.id = "value"+criterium_num.toString();
value_input.type = "text";
value_input.placeholder="Wert "+criterium_num.toString();
value_div=document.createElement("div");
value_div.className="autocomplete";
value_div.style="width:15%";
value_div.innerHTML=value_input.outerHTML;
// index
index_input=document.createElement("P");
index_input.id = "index"+criterium_num.toString();
index_input.innerText = "Q"+criterium_num.toString();
//index_input.placeholder="Wert "+criterium_num.toString();
index_div=document.createElement("div");
index_div.className="autocomplete";
index_div.style="width:5%";
index_div.innerHTML=index_input.outerHTML;
// white space
white_space=document.createElement("P");
white_space.innerText = "";
white_space_div=document.createElement("div");
white_space_div.className="autocomplete";
white_space_div.style="width:2%";
white_space_div.innerHTML=white_space.outerHTML;
ws=white_space_div.outerHTML;
var criterium=document.createElement("div");
criterium.display="block";
criterium.innerHTML=index_div.outerHTML+ws+signal_div.outerHTML+ws+feature_div.outerHTML+ws+operator_div.outerHTML+ws+value_div.outerHTML;
criteria.appendChild(criterium);
criteria_count+=1;
autocomplete(document.getElementById("signal"+criterium_num.toString()), signal_options);
dropdown(document.getElementById("feature"+criterium_num.toString()), feature_options);
dropdown(document.getElementById("operator"+criterium_num.toString()), operator_options);
}
// autocomplete functions
function autocomplete(inp, arr) {
/*the autocomplete function takes two arguments,
the text field element and an array of possible autocompleted values:*/
var currentFocus;
/*execute a function when someone writes in the text field:*/
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
/*close any already open lists of autocompleted values*/
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
/*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
/*append the DIV element as a child of the autocomplete container:*/
this.parentNode.appendChild(a);
/*for each item in the array...*/
for (i = 0; i < arr.length; i++) {
/*check if the item starts with the same letters as the text field value:*/
//if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
if (arr[i].toUpperCase().indexOf(val.toUpperCase())!== -1) {
/*create a DIV element for each matching element:*/
b = document.createElement("DIV");
/*make the matching letters bold:*/
//b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
//b.innerHTML += arr[i].substr(val.length);
b.innerHTML = arr[i].substr(0, arr[i].toUpperCase().indexOf(val.toUpperCase()));
b.innerHTML += "<strong>" + arr[i].substr(arr[i].toUpperCase().indexOf(val.toUpperCase()), val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length+arr[i].toUpperCase().indexOf(val.toUpperCase()))
//b.innerHTML += arr[i]; // not bold display
/*insert a input field that will hold the current array item's value:*/
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
/*execute a function when someone clicks on the item value (DIV element):*/
b.addEventListener("click", function(e) {
/*insert the value for the autocomplete text field:*/
inp.value = this.getElementsByTagName("input")[0].value;
/*close the list of autocompleted values,
(or any other open lists of autocompleted values:*/
closeAllLists();
});
a.appendChild(b);
}
}
});
/*execute a function presses a key on the keyboard:*/
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
/*If the arrow DOWN key is pressed,
increase the currentFocus variable:*/
currentFocus++;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 38) { //up
/*If the arrow UP key is pressed,
decrease the currentFocus variable:*/
currentFocus--;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 13) {
/*If the ENTER key is pressed, prevent the form from being submitted,*/
e.preventDefault();
if (currentFocus > -1) {
/*and simulate a click on the "active" item:*/
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
/*a function to classify an item as "active":*/
if (!x) return false;
/*start by removing the "active" class on all items:*/
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
/*add class "autocomplete-active":*/
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
/*a function to remove the "active" class from all autocomplete items:*/
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
/*close all autocomplete lists in the document,
except the one passed as an argument:*/
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
/*execute a function when someone clicks in the document:*/
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
// dropdown functions
function dropdown(inp, arr) {
/*the dropdown function takes two arguments,
the text field element and an array of possible autocompleted values:*/
var currentFocus;
/*execute a function when someone writes in the text field:*/
inp.addEventListener("mouseover", function(e) {
var a, b, i, val = this.value;
/*close any already open lists of autocompleted values*/
closeAllLists();
//if (!val) { return false;}
currentFocus = -1;
/*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
/*append the DIV element as a child of the autocomplete container:*/
this.parentNode.appendChild(a);
/*for each item in the array...*/
for (i = 0; i < arr.length; i++) {
/*check if the item starts with the same letters as the text field value:*/
//if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
/*create a DIV element for each matching element:*/
b = document.createElement("DIV");
b.innerHTML = arr[i];
/*insert a input field that will hold the current array item's value:*/
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
/*execute a function when someone clicks on the item value (DIV element):*/
b.addEventListener("click", function(e) {
/*insert the value for the autocomplete text field:*/
inp.value = this.getElementsByTagName("input")[0].value;
/*close the list of autocompleted values,
(or any other open lists of autocompleted values:*/
closeAllLists();
});
a.appendChild(b);
}
/*close dropdown after moving the pointer out of it*/
a.addEventListener("mouseleave", function(e) {
closeAllLists();
});
});
/*execute a function presses a key on the keyboard:*/
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
/*If the arrow DOWN key is pressed,
increase the currentFocus variable:*/
currentFocus++;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 38) { //up
/*If the arrow UP key is pressed,
decrease the currentFocus variable:*/
currentFocus--;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 13) {
/*If the ENTER key is pressed, prevent the form from being submitted,*/
e.preventDefault();
if (currentFocus > -1) {
/*and simulate a click on the "active" item:*/
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
/*a function to classify an item as "active":*/
if (!x) return false;
/*start by removing the "active" class on all items:*/
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
/*add class "autocomplete-active":*/
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
/*a function to remove the "active" class from all autocomplete items:*/
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
/*close all autocomplete lists in the document,
except the one passed as an argument:*/
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
/*execute a function when someone clicks in the document:*/
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
// Set Output variables as a ','-concatenated string of the manually selected signals
setInterval(function(){
FLOW_VARIABLES["debug"]=6;
//document.getElementById("debug").innerHTML = "Criteria Count: "+criteria_count.toString();
document.getElementById("debug").innerHTML = "";
/* actualize input variables every 0.2 seconds*/
FLOW_VARIABLES["signals"]='';
FLOW_VARIABLES["features"]='';
FLOW_VARIABLES["operators"]='';
FLOW_VARIABLES["values"]='';
FLOW_VARIABLES["indices"]='';
var i;
for (i = 1; i <= criteria_count; i++) {
FLOW_VARIABLES["signals"]+=document.getElementById("signal"+i.toString()).value;
FLOW_VARIABLES["features"]+=document.getElementById("feature"+i.toString()).value;
FLOW_VARIABLES["operators"]+=document.getElementById("operator"+i.toString()).value;
FLOW_VARIABLES["values"]+=document.getElementById("value"+i.toString()).value;
FLOW_VARIABLES["indices"]+=document.getElementById("index"+i.toString()).innerText;
if (i!=criteria_count){
FLOW_VARIABLES["signals"]+=',';
FLOW_VARIABLES["features"]+=',';
FLOW_VARIABLES["operators"]+=',';
FLOW_VARIABLES["values"]+=',';
FLOW_VARIABLES["indices"]+=',';
}
}
//define filter as user's input
if (document.getElementById("filt_advanced").value){
FLOW_VARIABLES["filter_def"]=document.getElementById("filter_def").value;
}
}, 100);
body {
font-family: sans-serif;
}
h1 {
font-size: 1.1em;
font-weight: bold;
}
.success {
color: #006633;
font-weight: bold;
}
.background {
color: #000000;
}
.filter_conf{
color: #ffffff;
display: block;
}
.buttons {
margin: 3px 8px;
border: 1px solid transparent;
padding: 7px;
font-size: 16px;
}
.failure {
color: #990000;
font-weight: bold;
}
* { box-sizing: border-box; }
body {
font: 16px Arial;
}
.autocomplete {
/*the container must be positioned relative:*/
position: relative;
display: inline-block;
}
input {
margin: 3px 8px;
border: 1px solid transparent;
background-color: #f1f1f1;
padding: 7px;
font-size: 16px;
}
input[type=radial] {
width: 3%;
}
input[type=text] {
background-color: #f1f1f1;
width: 100%;
}
input[type=submit] {
background-color: DodgerBlue;
color: #fff;
}
.autocomplete-items {
position: absolute;
border: 21px solid #d4d4d4;
border-bottom: none;
border-top: none;
z-index: 99;
/*position the autocomplete items to be the same width as the container:*/
top: 100%;
left: 0;
right: 0;
}
.autocomplete-items div {
padding: 10px;
cursor: pointer;
background-color: #fff;
border-bottom: 1px solid #d4d4d4;
}
.autocomplete-items div:hover {
/*when hovering an item:*/
background-color: #e9e9e9;
}
.autocomplete-active {
/*when navigating through the items using the arrow keys:*/
background-color: DodgerBlue !important;
color: #ffffff;
}
我找到了错误。您必须 increment/decrement criterium_num 在 onclick 事件中从 2 个按钮开始。要删除条件,您必须搜索相应的 div 并将其 outerHTML 设置为 ''。为了防止获得标准的负面索引,我将其设置为 1,如果它更小 1 通过多次单击 delte 按钮(删除太多)。
document.querySelector("#btn").onclick = function add_signal(event) {
if (criterium_num<1) criterium_num=1;
x = create_filter_criterium(criterium_num++);
arrayselection.push(x)
}
document.getElementById("delete").onclick = function(event) {
const index = arrayselection.indexOf(criterium_num--);
document.getElementById('criterium_'+criterium_num).outerHTML='';
arrayselection.splice(index, 1);
}
要找到这个 div,您需要在条件中添加一个 ID。
var criterium=document.createElement("div");
criterium.display="block";
criterium.id='criterium_'+criterium_num;
criterium.innerHTML=index_div.outerHTML+ws+signal_div.outerHTML+ws+feature_div.outerHTML+ws+operator_div.outerHTML+ws+value_div.outerHTML;
为了测试,您可以使用 https://jsfiddle.net/r2ya49cv/1/ 完整代码在哪里,而这里只列出了相关行。
请控制视图和标准中的索引是否正确,因为您使用的是生产系统。
备注:下次请缩短您的代码,仅针对相关部分。其他人搜索相关部分很繁琐,这使得它变得更加复杂。