接收Javascript中的重复值
我有以下代码:接收Javascript中的重复值,javascript,Javascript,我有以下代码: <html> <head> <link rel="stylesheet" href ="java.css"/> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> function meal(){ var checkboxes = document.getEl
<html>
<head>
<link rel="stylesheet" href ="java.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function meal(){
var checkboxes = document.getElementsByName('checkboxes');
var i=0;
var j=0;
var dish = [
["Boiled eggs","Eggs"],
["BLT Sandwich","Bacon"],
["BLT Sandwich","Lettuce"],
["BLT Sandwich","Tomato"],
["BLT Sandwich","Bread"],
["American Breakfast","Eggs"],
["American Breakfast","Bacon"],
["American Breakfast","Bread"],
["French Fries","Patatoes"]
];
var content = "";
for (i=0 ; i < dish.length; i++){
for(j=0 ; j< checkboxes.length; j++){
if ((dish[i][1] == checkboxes[j].value)&&(checkboxes[j].checked)){
content+= dish[i][0] + '<br>';
}
}
}
document.getElementById("result").innerHTML = content;
}
</script>
</head>
<body>
<div id="big_one">
<header id="Welcome">
<h1>Welcome</h1>
</header>
<div id= "new_div">
<section id="check_boxes">
<article >
<header>
<form name="ingredient">
<input type="checkbox" name="checkboxes" value="Eggs"> Eggs <br>
<input type="checkbox" name="checkboxes" value="Bacon"> Bacon <br>
<input type="checkbox" name="checkboxes" value="Lettuce"> Lettuce <br>
<input type="checkbox" name="checkboxes" value="Tomato"> Tomato <br>
<input type="checkbox" name="checkboxes" value="Bread"> Bread <br>
<input type="checkbox" name="checkboxes" value="Patatoes"> Patatoes </br>
</form>
</header>
</article>
<article >
<header>
<form name="Button">
<input type="button" name="Check" onClick="meal()" value="Search">
</form>
</header>
</article>
</section>
<div id="result">
</div>
</div>
<footer id="the_footer">
Copyright © Dejan Ivanov
</footer>
</div>
</body>
</html>
你能帮我在Javascript代码中解决这个问题吗。这应该能帮你解决问题: 如果对你有用,请告诉我 我不得不对您的html和javascript进行了一些更改: HTML:
欢迎
鸡蛋
培根
生菜
西红柿
面包
土豆
javascript:
// initialize the following lines once on the page so you don't have to do this every time you run the dish search:
// your data:
var allDishes = [
["Boiled eggs", ["Eggs"]],
["BLT Sandwich", ["Bacon", "Lettuce", "Tomato", "Bread"]],
["American Breakfast", ["Eggs", "Bacon", "Bread"]],
["French Fries", ["Potatoes"]]
];
// assemble a list of references to all of the checkboxes:
var checkboxes = document.querySelectorAll("input[type=checkbox]");
// tell the Search button to run the function "meal" when you click it:
document.getElementById("gatherIngredients").addEventListener("click", meal);
// this runs when the Search button is clicked:
function meal() {
// STEP 1: make a record of all the ingredients you've selected:
var selectedIngredients = [];
for (var i = 0; i < checkboxes.length; i++) {
// ensure that your text on the page matches exactly what the value is, so you won't get an error if you misspell one of the ingredients
var newVal = String(checkboxes[i].parentNode.textContent).replace(/^[ \W]+|[ \W]+$/, "");
checkboxes[i].value = newVal;
// alert(String("X" + newVal + "Y"));
//checkboxes[i].value = "sal";
// alert(checkboxes[i].value);
if (checkboxes[i].checked) {
// this item was checked, so add it to selectedIngredients
selectedIngredients.push(checkboxes[i].value);
}
}
// STEP 2: determine if each ingredient that was selected also has available all the other ingredients available to make any of the dishes
var dishesThatCanBeMade = [];
for (var eachIngredient = 0; eachIngredient < selectedIngredients.length; eachIngredient++) {
// Let's start off by assuming all ingredients needed for each dish are selected. If one ingredient is not selected, this turns false.
var allIngredientsAreAvailable = true;
var thisIngredient = selectedIngredients[eachIngredient];
// check each dish...
for (var eachDish = 0; eachDish < allDishes.length; eachDish++) {
var allIngredientsNeededForThisDish = allDishes[eachDish][1];
// does this dish have this selected ingredient?
if (allIngredientsNeededForThisDish.indexOf(thisIngredient) != -1) {
// alert(thisIngredient + " is an ingredient in " + allDishes[eachDish][0] + ", which calls for " + allIngredientsNeededForThisDish + ".");
// yes, this dish has this ingredient, but have all the other ingredients been checked off as well?
for (var everyOtherIngredient = 0; everyOtherIngredient < allIngredientsNeededForThisDish.length; everyOtherIngredient++) {
if (selectedIngredients.indexOf(allIngredientsNeededForThisDish[everyOtherIngredient]) == -1) {
allIngredientsAreAvailable = false;
break;
}
}
if (allIngredientsAreAvailable == true) {
// if the dish isn't already in the list, add it:
var thisDish = allDishes[eachDish][0];
if (dishesThatCanBeMade.indexOf(thisDish) == -1) {
dishesThatCanBeMade.push(thisDish);
}
}
}
}
}
var theResults = "With the ingredients available to you:<br>";
theResults += selectedIngredients.join(", ");
theResults += "<br><br>you can prepare the following dishes:<br>";
theResults += dishesThatCanBeMade.join(", ");
document.getElementById("result").innerHTML = theResults;
}
//在页面上初始化以下行一次,这样您就不必每次运行dish搜索时都这样做:
//您的数据:
var Alldisks=[
[“煮鸡蛋”、[“鸡蛋”],
[“BLT三明治”、“培根”、“生菜”、“西红柿”、“面包”],
[“美国早餐”[“鸡蛋”、“培根”、“面包”],
[“炸薯条”[“土豆”]]
];
//组合所有复选框的引用列表:
var checkbox=document.querySelectorAll(“输入[type=checkbox]”);
//当您单击搜索按钮时,告诉它运行功能“用餐”:
document.getElementById(“GatherComponents”).addEventListener(“点击”,菜单);
//单击“搜索”按钮时会运行此操作:
功能餐(){
//步骤1:记录您选择的所有成分:
var SelectedGredients=[];
对于(变量i=0;i”;
theResults+=SelectedGredients.join(“,”);
结果+=“
您可以准备以下菜肴:
”;
结果+=可以制作的盘。连接(“,”;
document.getElementById(“结果”).innerHTML=theResults;
}
只是想知道我的答案是否适用于您…?您好,dublicate值的问题已经解决,但选择的问题仍然存在。在我的计算机上,这个脚本不起作用。当我选择“鸡蛋”时,应该只看到“煮蛋”,但结果是“煮蛋”和“美国啤酒”。idia是指当我有鸡蛋时,我只能煮“煮蛋”。你能帮我解决这个问题吗?是的,我很乐意帮你。我今天做不到,你能等到明天吗?嗨,德扬。你能澄清一下选择问题是什么意思吗?在你的原始数据中,选择“鸡蛋”会显示“煮蛋”和“美国早餐”“.它在你的电脑上怎么不工作?你用的是什么浏览器?你知道密码在哪里被破解了吗?你也可以通过添加一些警报功能来改变我的小提琴。如果你能向我阐明你想在结果中显示什么的逻辑,这会有所帮助。鸡蛋不应该给出“煮蛋”和“美国早餐”的结果吗?请试着弄清楚为什么或者为什么不,然后我可以帮你更多。更具体地说,我会这样解释:如果冰箱里只有鸡蛋,我只能煮煮蛋。为了美国
<div id="big_one">
<header id="Welcome">
<h1>Welcome</h1>
</header>
<div id="new_div">
<section id="check_boxes">
<article>
<header>
<form name="ingredient">
<!--Wrapping the input in a <label> tag makes the word clickable, too, not just the checkbox-->
<label>
<input type="checkbox" value="">Eggs</label>
<br>
<label>
<input type="checkbox" value="">Bacon</label>
<br>
<label>
<input type="checkbox" value="">Lettuce</label>
<br>
<label>
<input type="checkbox" value="">Tomato</label>
<br>
<label>
<input type="checkbox" value="">Bread</label>
<br>
<label>
<input type="checkbox" value="">Potatoes</label>
<br>
</form>
</header>
</article>
<article>
<header>
<form>
<input id="gatherIngredients" type="button" onClick="meal();" value="Search">
</form>
</header>
</article>
</section>
<div id="result"></div>
</div>
// initialize the following lines once on the page so you don't have to do this every time you run the dish search:
// your data:
var allDishes = [
["Boiled eggs", ["Eggs"]],
["BLT Sandwich", ["Bacon", "Lettuce", "Tomato", "Bread"]],
["American Breakfast", ["Eggs", "Bacon", "Bread"]],
["French Fries", ["Potatoes"]]
];
// assemble a list of references to all of the checkboxes:
var checkboxes = document.querySelectorAll("input[type=checkbox]");
// tell the Search button to run the function "meal" when you click it:
document.getElementById("gatherIngredients").addEventListener("click", meal);
// this runs when the Search button is clicked:
function meal() {
// STEP 1: make a record of all the ingredients you've selected:
var selectedIngredients = [];
for (var i = 0; i < checkboxes.length; i++) {
// ensure that your text on the page matches exactly what the value is, so you won't get an error if you misspell one of the ingredients
var newVal = String(checkboxes[i].parentNode.textContent).replace(/^[ \W]+|[ \W]+$/, "");
checkboxes[i].value = newVal;
// alert(String("X" + newVal + "Y"));
//checkboxes[i].value = "sal";
// alert(checkboxes[i].value);
if (checkboxes[i].checked) {
// this item was checked, so add it to selectedIngredients
selectedIngredients.push(checkboxes[i].value);
}
}
// STEP 2: determine if each ingredient that was selected also has available all the other ingredients available to make any of the dishes
var dishesThatCanBeMade = [];
for (var eachIngredient = 0; eachIngredient < selectedIngredients.length; eachIngredient++) {
// Let's start off by assuming all ingredients needed for each dish are selected. If one ingredient is not selected, this turns false.
var allIngredientsAreAvailable = true;
var thisIngredient = selectedIngredients[eachIngredient];
// check each dish...
for (var eachDish = 0; eachDish < allDishes.length; eachDish++) {
var allIngredientsNeededForThisDish = allDishes[eachDish][1];
// does this dish have this selected ingredient?
if (allIngredientsNeededForThisDish.indexOf(thisIngredient) != -1) {
// alert(thisIngredient + " is an ingredient in " + allDishes[eachDish][0] + ", which calls for " + allIngredientsNeededForThisDish + ".");
// yes, this dish has this ingredient, but have all the other ingredients been checked off as well?
for (var everyOtherIngredient = 0; everyOtherIngredient < allIngredientsNeededForThisDish.length; everyOtherIngredient++) {
if (selectedIngredients.indexOf(allIngredientsNeededForThisDish[everyOtherIngredient]) == -1) {
allIngredientsAreAvailable = false;
break;
}
}
if (allIngredientsAreAvailable == true) {
// if the dish isn't already in the list, add it:
var thisDish = allDishes[eachDish][0];
if (dishesThatCanBeMade.indexOf(thisDish) == -1) {
dishesThatCanBeMade.push(thisDish);
}
}
}
}
}
var theResults = "With the ingredients available to you:<br>";
theResults += selectedIngredients.join(", ");
theResults += "<br><br>you can prepare the following dishes:<br>";
theResults += dishesThatCanBeMade.join(", ");
document.getElementById("result").innerHTML = theResults;
}