我有一个成分和所选成分的列表显示。当我点击一种成分时,我希望它出现在我选择的成分下,当我点击所选择的成分时,我希望它返回。(Javascript)按钮没有更新
这是我到目前为止。 http://jsfiddle.net/inspiredtolive/ghpus6on/3/
function forEach(array, action) {
for(var i=0; i<array.length; i++) {
action(array[i]);
}
}
function printIngredButtons(array, selected) {
forEach(array, function (ingredient) {
Buttons += '<button onclick="moveToOther(' + ingredient + ')">' + ingredient + '</button><br>';
});
if(!selected) {
document.getElementById("ingred").innerHTML = Buttons;
Buttons = "";
}
else {
document.getElementById("selected").innerHTML = Buttons;
Buttons = "";
}
}
function printAllButtons() {
printIngredButtons(ingredients, false);
printIngredButtons(selected, true);
}
function moveToOther(ingredient) {
var index = -1;
if(ingredients.indexOf(ingredient) > -1) {
ingredients.splice(index, 1);
selected.push(ingredient);
printAllButtons();
}
else if(selected.indexOf(ingredient) > -1){
selected.splice(index, 1);
ingredients.push(ingredient);
printAllButtons();
}
}
var ingredients = ["eggs", "cheese", "milk"];
var Buttons = "";
var selected = ["peanut butter"];
printAllButtons();
您必须提到代码中的问题,而不是倾销整个代码,并期望我们找到问题/清理。 – aadarshsg