Javascript Jquery切换下拉值
我一直在尝试创建一个jquery函数,该函数将为表行中的所有选择设置值。我想出了这个解决办法Javascript Jquery切换下拉值,javascript,jquery,Javascript,Jquery,我一直在尝试创建一个jquery函数,该函数将为表行中的所有选择设置值。我想出了这个解决办法 $(function () { $(".toogle").click(function () { if ($(this).hasClass("on") === true) { $(this).addClass("off"); $(this).removeClass("on"); } else {
$(function () {
$(".toogle").click(function () {
if ($(this).hasClass("on") === true) {
$(this).addClass("off");
$(this).removeClass("on");
} else {
$(this).addClass("on");
$(this).removeClass("off");
}
$(this).each(function () {
if ($(this).hasClass("on") === true) {
$(this).closest('tr').find('select').each(function () {
$(this).val("1");
});
} else {
$(this).closest('tr').find('select').each(function () {
$(this).val("0");
});
}
});
});
});
您可以在此处看到完整的代码:
但我想知道是否可以以更好的方式完成。这里有很多简化的空间:
这里有很多简化的空间:
这里有很多简化的空间:
这里有很多简化的空间:
如果是2状态字段,你应该使用复选框。我喜欢toogle这个词。如果是2状态字段,你应该使用复选框。我喜欢toogle这个词。如果是2状态字段,你应该使用复选框。我喜欢toogle这个词。如果是2状态字段,你应该使用复选框。我喜欢toogle这个词。谢谢!这正是我想要的,更少的代码,更容易维护:)谢谢!这正是我想要的,更少的代码,更容易维护:)谢谢!这正是我想要的,更少的代码,更容易维护:)谢谢!这正是我想要的,更少的代码,更易于维护:)
$(".toogle").click(function () {
$(this)
.toggleClass("on off")
.closest('tr')
.find('select')
.val($(this).hasClass("on") ? "1" : "0");
});