【发布时间】:2015-07-14 06:43:54
【问题描述】:
我正在使用复选框和输入来启用/禁用选择,我想知道我是否可以使用循环、变量或复合语句来简单地使用这个 js?只是感觉相对简单的功能需要很多代码。
这是我正在做的事情:
http://jsfiddle.net/kirkbross/555f2yan/1/
//check to see if checkboxes are checked and enable / disable accordingly
$(".zone-on-off").each(function() {
if (this.checked) {
$(this).parent("div").siblings("div").children("select").prop("disabled", false);
} else {
$(this).parent("div").siblings("div").children("select").prop("disabled", "disabled");
}
});
// enable / disable selects per on/off checkbox
$(".zone-on-off").click(function() {
if (this.checked) {
$(this).parent("div").siblings("div").children("select").prop("disabled", false);
} else {
$(this).parent("div").siblings("div").children("select").prop("disabled", "disabled");
}
});
// enable selects when name is inputted
$(".zone-name").change(function() {
if (this.val().length) {
$(this).parent("div").siblings("div").children("select").prop("disabled", false);
} else {
$(this).parent("div").siblings("div").children("select").prop("disabled", "disabled");
}
});
// input name on enter
$('.zone-name').keypress(function(e) {
if (e.which == 13) {
$(this).blur();
$(this).parent("div").siblings("div").children("select").prop("disabled", false);
}
});
【问题讨论】:
-
嗯...启用在输入名称时选择的部分似乎不起作用?-我想你想要 $(this).val。
-
您介意使用缩进吗?
-
这对于代码审查网站 (codereview.stackexchange.com) 来说可能是一个很好的问题,这是一个询问技术上可行的代码的好地方,但可能会以某种方式进行改进。只是一个想法!