gpt4 book ai didi

javascript - 如何在 1 个 if 语句中包含多个条件

转载 作者:行者123 更新时间:2023-11-28 01:56:13 28 4
gpt4 key购买 nike

我试图隐藏一个按钮,直到两个选择框都选择了一个项目。

<select id="comType">
<option>-- Select --</option>
<option>Call</option>
<option>Fax</option>
<option>Email</option>
</select>
<select id="comDirection">
<option>-- Select --</option>
<option>Incoming</option>
<option>Outgoing</option>
</select>

<a href="#" id="button_that_displays_only_when_both_selects_have_input">Next</a>

我目前正在使用的东西,但我知道这是不对的。

<script>
$(document).ajaxSuccess(function () {
if ($("#comType").change()) || ($("#comDirection").change()))
{ $("#button_that_displays_only_when_both_selects_have_input").show()}
});
</script>

如果这可以确保实际选择,那么这将是一个额外的好处,例如,不允许第一个选择选项计数,因为它们只是占位符......

谢谢,标记

最佳答案

// take both inputs and bind this function to the change event of both
$("#comType, #comDirection").on('change', function () {
// show the button if they both have a value
if ($("#comType").val().length > 0 && $("#comDirection").val().length > 0) {
$("#button_that_displays_only_when_both_selects_have_input").show();
}
});

由于条件是检查值长度,因此一旦正确设置了选项,只有在选择了实际值时才会显示按钮。

值的长度为0,按钮不会显示:

<option value="">Placeholder</option>

值的长度为3,因此按钮将显示(如果条件的另一侧也满足):

<option value="fax">Fax</option>

关于javascript - 如何在 1 个 if 语句中包含多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19079241/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com