如果这个 === 这个和这个 === 这个 show/hide div Javascript
IF this === this AND this === this show/hide div Javascript
我正在尝试创建一个 HTML/CSS/JS 表单以供离线使用。
到目前为止,我已经根据选择为 div
功能创建了 show()
和 hide()
。
如果选择 Cloth
和 White
显示一个 div
但我无法让它工作。
下面是我的尝试:
if ($("#StartSelector").val() == "Cloth") &&
if ($("#ColourField").val() == "White") {
$('#Program').show();
else {
$('#Program').hide();
}
};
form {
max-width: 900px;
display: block;
margin: 0 auto;
}
<form>
<div class="form-group">
<label for="StartSelector">What are you washing?</label>
<select class="form-control" id="StartSelector">
<option value="no">Select wash option</option>
<option value="Cloth">Table Cloth & Napkins</option>
<option value="HeavyDuty">Heavy Duty Work Wear</option>
<option value="Kitchen">Kitchen Work Wear</option>
<option value="Bed">Bed Linen</option>
<option value="Curtains">Curtains</option>
<option value="Bath">Bath Towels & Robes</option>
<option value="Uniforms">Uniforms</option>
<option value="Personal">Personal Clothing</option>
<option value="Mops">Mops & Cleaning Textiles</option>
<option value="Low">Filter for low temperatures</option>
</select>
</div>
<div class="form-group" id="Colours">
<label for="ColourDiv">Types of wash.</label>
<select class="form-control" id="ColourField">
<option value="no">Select wash option</option>
<option value="White">White</option>
<option value="Colours">Colours</option>
<option value="Delicate">Delicate</option>
</select>
</div>
<div class="form-group" id="Program" style="display: none;">
<img src="Program1.png" alt="Program 1" width="auto" height="400">
</div>
</form>
更新:感谢TiiJ7 and freedomn-m的意见和建议
您必须按值而不是 option
文本搜索。
该函数在 &&
运算符之后的 if
中有一个 if
,因此需要进行调整。
你必须有一个侦听器来捕获这些select
的变化并进行比较。
我假设您已经将 jquery 库添加到您的项目中。
function myFunction() {
if ($('#StartSelector :selected').val() == 'Cloth' &&
$('#ColourField :selected').val() == 'White') {
$('#Program').show();
} else {
$('#Program').hide();
}
}
$(function() {
$('#StartSelector, #ColourField').on('change', myFunction);
});
form {
max-width: 900px;
display: block;
margin: 0 auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<form>
<div class="form-group">
<label for="StartSelector">What are you washing?</label>
<select class="form-control" id="StartSelector">
<option value="no">Select wash option</option>
<option value="Cloth">Table Cloth & Napkins</option>
<option value="HeavyDuty">Heavy Duty Work Wear</option>
<option value="Kitchen">Kitchen Work Wear</option>
<option value="Bed">Bed Linen</option>
<option value="Curtains">Curtains</option>
<option value="Bath">Bath Towels & Robes</option>
<option value="Uniforms">Uniforms</option>
<option value="Personal">Personal Clothing</option>
<option value="Mops">Mops & Cleaning Textiles</option>
<option value="Low">Filter for low temperatures</option>
</select>
</div>
<div class="form-group" id="Colours">
<label for="ColourDiv">Types of wash.</label>
<select class="form-control" id="ColourField">
<option value="no">Select wash option</option>
<option value="White">White</option>
<option value="Colours">Colours</option>
<option value="Delicate">Delicate</option>
</select>
</div>
<div class="form-group" id="Program" style="display: none;">
<img src="Program1.png" alt="Program 1" width="auto" height="400">
</div>
</form>
我正在尝试创建一个 HTML/CSS/JS 表单以供离线使用。
到目前为止,我已经根据选择为 div
功能创建了 show()
和 hide()
。
如果选择 Cloth
和 White
显示一个 div
但我无法让它工作。
下面是我的尝试:
if ($("#StartSelector").val() == "Cloth") &&
if ($("#ColourField").val() == "White") {
$('#Program').show();
else {
$('#Program').hide();
}
};
form {
max-width: 900px;
display: block;
margin: 0 auto;
}
<form>
<div class="form-group">
<label for="StartSelector">What are you washing?</label>
<select class="form-control" id="StartSelector">
<option value="no">Select wash option</option>
<option value="Cloth">Table Cloth & Napkins</option>
<option value="HeavyDuty">Heavy Duty Work Wear</option>
<option value="Kitchen">Kitchen Work Wear</option>
<option value="Bed">Bed Linen</option>
<option value="Curtains">Curtains</option>
<option value="Bath">Bath Towels & Robes</option>
<option value="Uniforms">Uniforms</option>
<option value="Personal">Personal Clothing</option>
<option value="Mops">Mops & Cleaning Textiles</option>
<option value="Low">Filter for low temperatures</option>
</select>
</div>
<div class="form-group" id="Colours">
<label for="ColourDiv">Types of wash.</label>
<select class="form-control" id="ColourField">
<option value="no">Select wash option</option>
<option value="White">White</option>
<option value="Colours">Colours</option>
<option value="Delicate">Delicate</option>
</select>
</div>
<div class="form-group" id="Program" style="display: none;">
<img src="Program1.png" alt="Program 1" width="auto" height="400">
</div>
</form>
更新:感谢TiiJ7 and freedomn-m的意见和建议
您必须按值而不是 option
文本搜索。
该函数在 &&
运算符之后的 if
中有一个 if
,因此需要进行调整。
你必须有一个侦听器来捕获这些select
的变化并进行比较。
我假设您已经将 jquery 库添加到您的项目中。
function myFunction() {
if ($('#StartSelector :selected').val() == 'Cloth' &&
$('#ColourField :selected').val() == 'White') {
$('#Program').show();
} else {
$('#Program').hide();
}
}
$(function() {
$('#StartSelector, #ColourField').on('change', myFunction);
});
form {
max-width: 900px;
display: block;
margin: 0 auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<form>
<div class="form-group">
<label for="StartSelector">What are you washing?</label>
<select class="form-control" id="StartSelector">
<option value="no">Select wash option</option>
<option value="Cloth">Table Cloth & Napkins</option>
<option value="HeavyDuty">Heavy Duty Work Wear</option>
<option value="Kitchen">Kitchen Work Wear</option>
<option value="Bed">Bed Linen</option>
<option value="Curtains">Curtains</option>
<option value="Bath">Bath Towels & Robes</option>
<option value="Uniforms">Uniforms</option>
<option value="Personal">Personal Clothing</option>
<option value="Mops">Mops & Cleaning Textiles</option>
<option value="Low">Filter for low temperatures</option>
</select>
</div>
<div class="form-group" id="Colours">
<label for="ColourDiv">Types of wash.</label>
<select class="form-control" id="ColourField">
<option value="no">Select wash option</option>
<option value="White">White</option>
<option value="Colours">Colours</option>
<option value="Delicate">Delicate</option>
</select>
</div>
<div class="form-group" id="Program" style="display: none;">
<img src="Program1.png" alt="Program 1" width="auto" height="400">
</div>
</form>