在HTML中,没有三态复选按钮(是、否、空值),对吗?
有没有简单的技巧或解决方法,而不必自己渲染整个按钮?
在HTML中,没有三态复选按钮(是、否、空值),对吗?
有没有简单的技巧或解决方法,而不必自己渲染整个按钮?
编辑 — 感谢 Janus Troelsen 的评论,我找到了更好的解决方案:
indeterminate
的属性请参阅w3c reference guide。要使复选框在视觉上呈现不确定状态,请将其设置为 true:
element.indeterminate = true;
这里是Janus Troelsen的fiddle。但请注意:
indeterminate
状态无法在HTML标记中设置,只能通过Javascript来实现(参见这个JSfiddle test和这篇CSS tricks详细文章)。
此状态不会改变复选框的值,它只是一个视觉提示,掩盖了输入的真实状态。
浏览器测试:在Chrome 22、Firefox 15、Opera 12和IE7上测试成功。关于移动浏览器,Android 2.0浏览器和iOS 3.1上的Safari移动版不支持它。
之前的答案
Another alternative would be to play with the checkbox transparency for the "some selected" state (as Gmail
doesused to do in previous versions). It will require some javascript and a CSS class. Here I put a particular example that handles a list with checkable items and a checkbox that allows to select all/none of them. This checkbox shows a "some selected" state when some of the list items are selected.Given a checkbox with an ID
#select_all
and several checkboxes with a class.select_one
,The CSS class that fades the "select all" checkbox would be the following:
.some_selected { opacity: 0.5; filter: alpha(opacity=50); }
And the JS code that handles the tri-state of the select all checkbox is the following:
$('#select_all').change (function () { //Check/uncheck all the list's checkboxes $('.select_one').attr('checked', $(this).is(':checked')); //Remove the faded state $(this).removeClass('some_selected'); }); $('.select_one').change (function () { if ($('.select_one:checked').length == 0) $('#select_all').removeClass('some_selected').attr('checked', false); else if ($('.select_one:not(:checked)').length == 0) $('#select_all').removeClass('some_selected').attr('checked', true); else $('#select_all').addClass('some_selected').attr('checked', true); });
You can try it here: http://jsfiddle.net/98BMK/
.prop('checked', ...)
而不是.attr('checked', ...)
。 - Lunaindeterminate
IDL属性。我的建议是使用三个适当的Unicode字符来表示三种状态,例如❓、✅、❌
请参见示例:
/**
* loops thru the given 3 values for the given control
*/
function tristate(control, value1, value2, value3) {
switch (control.value.charAt(0)) {
case value1:
control.value = value2;
break;
case value2:
control.value = value3;
break;
case value3:
control.value = value1;
break;
default:
// display the current value if it's unexpected
alert(control.value);
}
}
function tristate_Marks(control) {
tristate(control,'\u2753', '\u2705', '\u274C');
}
function tristate_Circles(control) {
tristate(control,'\u25EF', '\u25CE', '\u25C9');
}
function tristate_Ballot(control) {
tristate(control,'\u2610', '\u2611', '\u2612');
}
function tristate_Check(control) {
tristate(control,'\u25A1', '\u2754', '\u2714');
}
<input type='text'
style='border: none;'
onfocus='this.blur()'
readonly='true'
size='1'
value='❓' onclick='tristate_Marks(this)' />
<input style="border: none;"
id="tristate"
type="text"
readonly="true"
size="1"
value="❓"
onclick="switch(this.form.tristate.value.charAt(0)) {
case '❓': this.form.tristate.value='✅'; break;
case '✅': this.form.tristate.value='❌'; break;
case '❌': this.form.tristate.value='❓'; break;
};" />
<input type="radio" name="choice" value="yes" />Yes
<input type="radio" name="choice" value="No" />No
<input type="radio" name="choice" value="null" />null
像@Franz的回答一样,您也可以使用
参考@BoltClock的答案,下面是我对于一种更加复杂的递归方法的解决方案:
http://jsfiddle.net/gx7so2tq/2/
我可能不是最好的解决方案,但对我来说很有效,并且非常灵活。
我使用两个数据对象来定义容器:
data-select-all="chapter1"
以及元素本身:
data-select-some="chapter1"
两者具有相同的值。将两个数据对象组合在一个复选框中允许子级,这些子级被递归扫描。因此需要两个“辅助”函数来防止更改触发器。