更简单的显示、隐藏方式&禁用元素
Simpler way of showing, hiding & disabling elements
我现在有这个代码&寻找一种更简单、更短的显示、隐藏&禁用我的元素。。。
$("#chReportData").click(function () {
if ($(this)[0].checked) {
$("#reportDataOptions").show();
} else {
$("#ReportDataStatusOptions").hide();
$("#reportDataOptions").hide();
$('#chkReportPermission').attr('checked', false);
$('#chReportDataStatus').attr('checked', false);
$('#chReportDataCummulative').attr('checked', false);
$('.allowedUpload').attr('checked', false);
$('.allowedDelete').attr('checked', false);
}
});
$("#chReportDataStatus").click(function () {
if ($(this)[0].checked) {
$("#ReportDataStatusOptions").show();
} else if ($('#chReportDataCummulative').is('checked')) {
$("#ReportDataStatusOptions").hide();
$('.allowedUpload').attr('checked', false);
$('.allowedDelete').attr('checked', false);
} else {
$("#ReportDataStatusOptions").hide();
$('.allowedUpload').attr('checked', false);
$('.allowedDelete').attr('checked', false);
}
});
它运行良好,我只是在寻找一种更简单的方法。。。如果你知道一个较短的&更简单的方式,请分享。。。
您可以使用toggle
,而不是使用带有布尔值检查的show
和hide
。
jQuery切换可以用于切换项目的可见性,如下所示:$( ".target" ).toggle();
使用逗号分隔的多个选择器
$("#ReportDataStatusOptions , #reportDataOptions").hide();
$('#chkReportPermission , #chReportDataStatus , #chReportDataCummulative , .allowedUpload , .allowedDelete').attr('checked', false);
您可以采用模块化方法。
在函数中编写常见的东西,并在需要的地方调用它们。
它在维护代码的同时也很有帮助。
这是您的简化代码:
$("#chReportData").click(function () {
if ($(this)[0].checked) {
$("#reportDataOptions").show();
} else {
hide_ReportDataStatusOptions();
$("#reportDataOptions").hide();
uncheck_chReportRbtns();
uncheckAllowedRbtns();
}
});
$("#chReportDataStatus").click(function () {
if ($(this)[0].checked) {
$("#ReportDataStatusOptions").show();
} else if ($('#chReportDataCummulative').is('checked')) {
hide_ReportDataStatusOptions();
uncheckAllowedRbtns();
} else {
hide_ReportDataStatusOptions();
uncheckAllowedRbtns();
}
});
以及相应的功能:
function uncheck_AllowedRbtns(){
$('.allowedUpload, .allowedDelete').attr('checked', false);
}
function uncheck_chReportRbtns(){
var txt = ['Permission', 'DataStatus', 'DataCummulative'];
for(var i=0; i<txt.length; i++){
$('#chReport'+txt[i]).attr('checked', false);
}
}
function hide_ReportDataStatusOptions(){
$("#ReportDataStatusOptions").hide();
}
相关文章:
- 指令的模板必须只有一个根元素:With restrict E&替换true
- HTML5-画布元素&自定义光标
- Javascript链接-试图设置元素值-奇怪的行为chrome&安卓
- 将点击事件绑定到新元素&使得ID'是独一无二的
- 无法获取interact.js拖动&放弃交互以使用Boostrap元素
- 组内jquery选择器的问题&在元素上循环
- 关于在动态创建的元素中添加预先样式化的类或在那里添加stying&然后
- 元素不在数组javascript&替换数组
- 阅读&更改SnapSVG中元素的旋转值
- 我应该如何定义一个重复出现的HTML元素&用JavaScript找到它们
- 自动播放HTML5音频元素&动画
- 如何移动选定元素's值&文本到另一个选择元素
- 流星.js&mongodb-如何在每个空格中传递当前元素的id
- 添加、删除&清除表中的HTML元素
- jQuery/JavaScript如何处理绑定到同一元素的多个事件处理程序&事件,以及后果
- 获取元素&对于每个功能
- 如何循环元素&数数
- Chrome扩展:显示&隐藏元素
- 使用setTimeout函数&随机函数.以设置类元素的动画
- Javascript-将具有相同类的元素推入数组&在循环中使用每个