[英]Toggle between two checkboxes
I have two check boxes. 我有两个复选框。 One for showing a column and the other for showing an item in the column.
一个用于显示列,另一个用于显示列中的项目。 A user can check to make a column name visible without having to checking the box to show an item within the column.
用户可以检查以使列名可见,而无需选中该框以在列中显示项目。 But if the user checks the box to show an item of a column.
但是,如果用户选中该框以显示某列项目。 The checkbox for showing the column must be checked because you cannot make an item visible within a column without making the column visible as well.
必须选中用于显示列的复选框,因为在不使列也可见的情况下,您无法在列中显示项目。
I am having some trouble thinking of a logic that will do that without going in circles. 我在思考一种逻辑时会遇到一些麻烦,该逻辑无需绕圈就能做到。
<div>
<div class="span3">Column is visible:</div>
<div class="span9"><input type="checkbox" data-name="viewable" data-bind="checked: columnViewable" /></div>
</div>
<div>
<div class="span3">Column items are visible:</div>
<div class="span9"><input type="checkbox" data-name="viewableItems" data-bind="checked: columnItemsViewable" /></div>
</div>
This is what i have for js: 这就是我对js的要求:
editElement.editElement = function () {
if (editElement.viewableItems()) {
editElement.columnViewable = ko.observable(true);
var columnView = editElement.columnViewable();
}
else
columnView = editElement.columnViewable();
grain.Ajax.Post({..submit data including checked boxes...})
};
It is working fine for me if both boxes are unchecked and if i check the viewableItems and save it will automatically set the column to true. 如果两个框都未选中,并且我检查viewableItems并保存,则对我来说工作正常,它将自动将列设置为true。 But if both are checked and i uncheck the column visibility and save - it will recheck the column visibility because the viewableItems is checked.
但是,如果两者都选中,并且我取消选中列的可见性并保存-它会重新检查列的可见性,因为viewableItems已选中。
Should i disable the viewableItems checkbox if the viewable column is unchecked that way both will be set to false? 如果未选中viewable列,则是否应该禁用viewableItems复选框,那样两者都将设置为false?
You don't need a whole lot of logic for this. 您不需要很多逻辑。 Here's a VERY simple jQuery solution:
这是一个非常简单的jQuery解决方案:
$('#column,#item').hide(); function displayCol(check) { if (check) { $('#column').show(); } else { $('#column').hide(); $('#itemChanger').prop('checked',false); } } function displayItem(check) { if (check) { $('#column,#item').show(); $('#columnChanger').prop('checked',true); } else { $('#item').hide(); } }
#column { width: 200px; height: 200px; background: green; } #item { width: 100px; height: 100px; background: blue; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="column">Column <div id="item">Item</div> </div> Show Column: <input type="checkbox" id="columnChanger" onchange="displayCol(this.checked)" /> <br/>Show Item: <input type="checkbox" id="itemChanger" onchange="displayItem(this.checked)" />
The way I would do this is by disabling the Show Items input if Show columns is unchecked, and by trying for both controls to run as independently from one another as possible: 我这样做的方式是,如果未选中“显示”列,则禁用“显示项目”输入,并尝试使两个控件尽可能独立地运行:
<div class="column">
<h1 class="title">Column 1</h1>
<p class="item">Item 1</p>
<p class="item">Item 2</p>
<p class="item">Item 3</p>
<p class="item">Item 4</p>
<div class="controls">
<input id="show-items-1" class="show-items" type="checkbox" checked="checked" />
<label for="show-items-1">Show items</label>
<br/>
<input id="show-column-1" class="show-column" type="checkbox" checked="checked" />
<label for="show-column-1">Show column</label>
</div>
</div>
<div class="column">
<h1 class="title">Column 2</h1>
<p class="item">Item 1</p>
<p class="item">Item 2</p>
<p class="item">Item 3</p>
<p class="item">Item 4</p>
<div class="controls">
<input id="show-items-2" class="show-items" type="checkbox" checked="checked" />
<label for="show-items-2">Show items</label>
<br/>
<input id="show-column-2" class="show-column" type="checkbox" checked="checked" />
<label for="show-column-2">Show column</label>
</div>
</div>
$("input", this).on("change", function () {
var state = $(this).is(":checked"),
showColumn = $(this).attr("class").search(/column/) > -1,
elems = $(this).parents(".column").find("p");
if (state) {
if (showColumn) {
$(this).siblings(".show-items").removeAttr("disabled");
elems = $(this).siblings(".show-items").is(":checked") ? elems = $(this).parents(".column").find("h1,p") : $(this).parents(".column").find("h1");
}
elems.removeClass("hidden");
} else {
if (showColumn) {
$(this).siblings(".show-items").attr("disabled", true);
elems = $(this).parents(".column").find("h1, p");
}
elems.addClass("hidden");
}
});
Here is a working JSFiddle for reference. 这是一个工作中的JSFiddle供参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.