英文:
Checking if two boxes are selected in Javascript
问题
这是您提供的代码,以下是代码的翻译部分:
我正在尝试构建一个测试网站,基本上是这样说的:如果您选择花生复选框,这是一个不含花生的网站。如果您选择腰果,这是一个不含腰果的网站。如果您选择两者,这是一个不含花生和腰果的网站。 (将来,我还会添加其他食物的复选框,然后进行各种组合)但是,我在最后一部分遇到了问题 - 尝试在同时选择两个复选框时返回唯一的信息。
这是我编写的代码。没有最后的else if语句,它是有效的。但是我不确定最后的else if应该做什么才能使它完全运行。感谢任何指导!
<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->
<!-- 语言:lang-js -->
function myFunction() {
if (document.getElementById("peanuts").checked) {
document.write(
"<p>您已选择花生。这是一个不含花生的食谱:"
);
} else if (document.getElementById("cashews").checked) {
document.write(
"<p>您已选择腰果。这是一个不含腰果的食谱:"
);
} else if (document.getElementById("dairy").checked) {
document.write(
"<p>您已选择乳制品。这是一个不含乳制品的食谱:"
);
} else if (
document.getElementById("peanuts").checked &&
document.getElementById("cashews").checked
) {
document.write("<p>您已选择两者。这是一个不含坚果的食谱:");
}
}
<!-- 语言:lang-html -->
<input type="checkbox" name="peanuts" id="peanuts" />花生<br />
<input type="checkbox" name="cashews" id="cashews" />腰果<br />
<input type="checkbox" name="dairy" id="dairy" />乳制品<br />
<button id="myButton" type="button" value="getValue" onclick="myFunction()">
获取数值
</button>
<!-- 结束代码片段 -->
请注意,这是代码的翻译部分,不包括问题的回答。如果您有任何其他问题或需要进一步的协助,请随时提出。
英文:
I'm trying to build out a test website that basically says: if you select the peanuts checkbox, here's a peanut free website. If you select cashews, here's a cashew free website. If you select both, here's a peanut and cashew free website. (In the future, I would add other checkboxes as well for other foods, and then do various combinations) I'm getting stuck on the last part, however - trying to return unique info if both checkboxes are selected.
Here's the code I put together. Without the last else if statement, it works. But I'm not sure what I need to do with the last else if to make it function fully. Appreciate any guidance here!
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function myFunction() {
if (document.getElementById("peanuts").checked) {
document.write(
"<p> You have selected Peanuts. Here's a peanut-free recipe:"
);
} else if (document.getElementById("cashews").checked) {
document.write(
"<p> You have selected Cashews. Here's a cashew-free recipe:"
);
} else if (document.getElementById("dairy").checked) {
document.write(
"<p> You have selected Dairy. Here's a dairy-free recipe:"
);
} else if (
document.getElementById("peanuts").checked &&
document.getElementById("cashews").checked
) {
document.write("<p> You have selected Both. Here's a nut-free recipe:");
}
}
<!-- language: lang-html -->
<input type="checkbox" name="peanuts" id="peanuts" />Peanuts<br />
<input type="checkbox" name="cashews" id="cashews" />Cashews<br />
<input type="checkbox" name="dairy" id="dairy" />Dairy<br />
<button id="myButton" type="button" value="getValue" onclick="myFunction()">
Get Value
</button>
<!-- end snippet -->
答案1
得分: 0
将if条件写在"checked"的值上,我们可以将其存储在一个变量中。
将所有的.checked()值存储在一个变量中。
然后检查是否有任何两个值的组合为true。
如果需要代码,请告诉我。
英文:
instead of writing the if condition on the value of checked, we can store it in a variable.
Store all the .checked() values in a variable.
Then check if any combination of two values are true.
Let me know if you need the code for it.
答案2
得分: 0
这是因为执行顺序只需反转:
function myFunction() {
if (
document.getElementById("peanuts").checked &&
document.getElementById("cashews").checked
) {
document.write("<p>你选择了两者。这里有一个无坚果的食谱:");
} else if (document.getElementById("peanuts").checked) {
document.write(
"<p>你选择了花生。这里有一个无花生的食谱:"
);
} else if (document.getElementById("cashews").checked) {
document.write(
"<p>你选择了腰果。这里有一个无腰果的食谱:"
);
} else if (document.getElementById("dairy").checked) {
document.write(
"<p>你选择了乳制品。这里有一个无乳制品的食谱:"
);
}
}
这段代码未经过优化。
英文:
this is because the order of execution just needs to be reversed:
function myFunction() {
if (
document.getElementById("peanuts").checked &&
document.getElementById("cashews").checked
) {
document.write("<p> You have selected Both. Here's a nut-free recipe:");
} else if (document.getElementById("peanuts").checked) {
document.write(
"<p> You have selected Peanuts. Here's a peanut-free recipe:"
);
} else if (document.getElementById("cashews").checked) {
document.write(
"<p> You have selected Cashews. Here's a cashew-free recipe:"
);
} else if (document.getElementById("dairy").checked) {
document.write(
"<p> You have selected Dairy. Here's a dairy-free recipe:"
);
}
}
> this code is not optimized
答案3
得分: 0
你可以使用以下查询来查找所有选中的复选框:
document.querySelectorAll('input:checked')
并根据所选内容准备输出。
function myFunction() {
const selected = [...document.querySelectorAll('input.food:checked')];
if (selected.length < 1) {
return;
}
if (selected.length === 1) {
document.write(`<p> You have selected ${selected[0].name}. Here's a ${selected[0].name}-free recipe:`);
return;
}
document.write('<p> You have selected multiple: ' + selected.map(input => input.name).join(', '));
}
<input type="checkbox" name="peanuts" class="food" />Peanuts<br />
<input type="checkbox" name="cashews" class="food" />Cashews<br />
<input type="checkbox" name="dairy" class="food" />Dairy<br />
<button id="myButton" type="button" value="getValue" onclick="myFunction()">
Get Value
</button>
英文:
You can find all selected checkboxes with a query:
document.querySelectorAll('input:checked')
and prepare output based on what is selected.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function myFunction() {
const selected = [...document.querySelectorAll('input.food:checked')];
if (selected.length < 1) {
return;
}
if (selected.length === 1) {
document.write(`<p> You have selected ${selected[0].name}. Here's a ${selected[0].name}-free recipe:`);
return;
};
document.write('<p> You have selected multiple: ' + selected.map(input => input.name).join(', '));
}
<!-- language: lang-html -->
<input type="checkbox" name="peanuts" class="food" />Peanuts<br />
<input type="checkbox" name="cashews" class="food" />Cashews<br />
<input type="checkbox" name="dairy" class="food" />Dairy<br />
<button id="myButton" type="button" value="getValue" onclick="myFunction()">
Get Value
</button>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论