html - 使用 JQuery 和 HTML 显示下拉菜单
问题描述
I would like to show the class="residential-options" when the option residential is selected.
这是我的 HTML:
<div class= "row">
<select id='building'>
<option disabled selected value> -- select an option -- </option>
<option id="residential" value="residential" [...]>Residential</option>
<option id="commercial" value="commercial " [...]>Commercial</option>
<option id="corporate" value="corporate" [...]>Corporate</option>
<option id="hybrid" value="hybrid" [...]>Hybrid</option>
</select>
</div>
<div class="residential-options">
<div id="number-of-apartments" >
<label>N. of Apartments *</label>
<input [...]>
</div>
<div id="number-of-floors" >
<label>N. of Floors *</label>
<input [...]>
</div>
<div id="number-of-basements" >
<label>N. of Basements *</label>
<input [...]>
</div>
</div>
到目前为止,这是我的 jQuery:
window.onload = function() {
$('.residential-options').hide();
$('.commercial-options').hide();
$('.corporate-options').hide();
$('.hybrid-options').hide();
};
$("#residential").change(function() {
($(this).isChecked())
$('.residential-options').show();
$('.commercial-options').hide();
$('.corporate-options').hide();
$('.hybrid-options').hide();
});
$("#residential").trigger("change");
我认为问题可能来自我的 ($(this).isChecked()) 行,但我不确定。谢谢!
解决方案
您需要实现选择标签的更改事件并更改获取常驻选项的方式
if($(this).val() == 'residential'){
}
并更改检查选项的方式
if($(this).val() == 'residential'){
}
window.onload = function() {
$('.residential-options').hide();
$('.commercial-options').hide();
$('.corporate-options').hide();
$('.hybrid-options').hide();
};
$("#building").change(function() {
//($(this).isChecked())
if($(this).val() == 'residential'){
$('.residential-options').show();
$('.commercial-options').hide();
$('.corporate-options').hide();
$('.hybrid-options').hide();
}
});
$("#residential").trigger("change");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class= "row">
<select id='building'>
<option disabled selected value> -- select an option -- </option>
<option id="residential" value="residential" [...]>Residential</option>
<option id="commercial" value="commercial " [...]>Commercial</option>
<option id="corporate" value="corporate" [...]>Corporate</option>
<option id="hybrid" value="hybrid" [...]>Hybrid</option>
</select>
</div>
<div class="residential-options">
<div id="number-of-apartments" >
<label>N. of Apartments *</label>
<input [...]>
</div>
<div id="number-of-floors" >
<label>N. of Floors *</label>
<input [...]>
</div>
<div id="number-of-basements" >
<label>N. of Basements *</label>
<input [...]>
</div>
</div>
推荐阅读
- ansible-awx - Ansible AWX/Tower 创建仅限于组的库存
- python - 模型字段的 Django 时间差总和
- php - 提取文件名中包含非拉丁字符的 zip 文件时的 PHP 问题
- javascript - 如何从输入日期 vanilla JS 中排除日期
- javascript - 使用 Promise 和 foreach 异步返回
- node.js - 如何在不使用 npx 的情况下使用 npm-force-resolutions 强制 npm 依赖项?
- node.js - Node.js 中的 HTTP/2 POST 请求
- python - 是否有一个 python 函数可以为我提供“最小列表”?
- python - 如何使用 pythonic 代码在目录中查找特定文件?
- python-3.x - 使用 python 的 dbus 模块,是否可以更新通知的内容而不会再次弹出?