检查 JavaScript 中是否选择了两个框。

huangapple go评论67阅读模式
英文:

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(&quot;peanuts&quot;).checked) {
        document.write(
            &quot;&lt;p&gt; You have selected Peanuts. Here&#39;s a peanut-free recipe:&quot;
        );
    } else if (document.getElementById(&quot;cashews&quot;).checked) {
        document.write(
            &quot;&lt;p&gt; You have selected Cashews. Here&#39;s a cashew-free recipe:&quot;
        );
    } else if (document.getElementById(&quot;dairy&quot;).checked) {
        document.write(
            &quot;&lt;p&gt; You have selected Dairy. Here&#39;s a dairy-free recipe:&quot;
        );
    } else if (
        document.getElementById(&quot;peanuts&quot;).checked &amp;&amp;
        document.getElementById(&quot;cashews&quot;).checked
    ) {
        document.write(&quot;&lt;p&gt; You have selected Both. Here&#39;s a nut-free recipe:&quot;);
    }
}

<!-- language: lang-html -->

&lt;input type=&quot;checkbox&quot; name=&quot;peanuts&quot; id=&quot;peanuts&quot; /&gt;Peanuts&lt;br /&gt;

&lt;input type=&quot;checkbox&quot; name=&quot;cashews&quot; id=&quot;cashews&quot; /&gt;Cashews&lt;br /&gt;

&lt;input type=&quot;checkbox&quot; name=&quot;dairy&quot; id=&quot;dairy&quot; /&gt;Dairy&lt;br /&gt;

&lt;button id=&quot;myButton&quot; type=&quot;button&quot; value=&quot;getValue&quot; onclick=&quot;myFunction()&quot;&gt;
    Get Value
&lt;/button&gt;

<!-- 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(&quot;peanuts&quot;).checked &amp;&amp;
        document.getElementById(&quot;cashews&quot;).checked
    ) {
        document.write(&quot;&lt;p&gt; You have selected Both. Here&#39;s a nut-free recipe:&quot;);
    } else if (document.getElementById(&quot;peanuts&quot;).checked) {
        document.write(
            &quot;&lt;p&gt; You have selected Peanuts. Here&#39;s a peanut-free recipe:&quot;
        );
    } else if (document.getElementById(&quot;cashews&quot;).checked) {
        document.write(
            &quot;&lt;p&gt; You have selected Cashews. Here&#39;s a cashew-free recipe:&quot;
        );
    } else if (document.getElementById(&quot;dairy&quot;).checked) {
        document.write(
            &quot;&lt;p&gt; You have selected Dairy. Here&#39;s a dairy-free recipe:&quot;
        );
    }
}

> 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(&#39;input:checked&#39;)

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(&#39;input.food:checked&#39;)];
    if (selected.length &lt; 1) {
      return;
    }
    if (selected.length === 1) {
      document.write(`&lt;p&gt; You have selected ${selected[0].name}. Here&#39;s a ${selected[0].name}-free recipe:`);
      return;
    };
    document.write(&#39;&lt;p&gt; You have selected multiple: &#39; + selected.map(input =&gt; input.name).join(&#39;, &#39;));
}

<!-- language: lang-html -->

&lt;input type=&quot;checkbox&quot; name=&quot;peanuts&quot; class=&quot;food&quot; /&gt;Peanuts&lt;br /&gt;

&lt;input type=&quot;checkbox&quot; name=&quot;cashews&quot; class=&quot;food&quot; /&gt;Cashews&lt;br /&gt;

&lt;input type=&quot;checkbox&quot; name=&quot;dairy&quot; class=&quot;food&quot; /&gt;Dairy&lt;br /&gt;

&lt;button id=&quot;myButton&quot; type=&quot;button&quot; value=&quot;getValue&quot; onclick=&quot;myFunction()&quot;&gt;
    Get Value
&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 13:17:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定