在HTML中,没有三态复选按钮(是、否、空值),对吗?
有没有简单的技巧或解决方法,而不必自己渲染整个按钮?
在HTML中,没有三态复选按钮(是、否、空值),对吗?
有没有简单的技巧或解决方法,而不必自己渲染整个按钮?
indeterminate
的简短代码片段:
cb1.state = 1
function toggle_tristate(cb) {
cb.state = ++cb.state % 3 // cycle through 0,1,2
if (cb.state == 0) {
cb.indeterminate = true;
cb.checked = true; // after 'indeterminate' the state 'false' follows
}
}
<input id="cb1" type="checkbox" onclick="toggle_tristate(this)">
只有当 state==0
时才会被捕获,其余情况都会被自动处理。
这是一个使用简单的jQuery和属性data-checked
的示例:
$("#checkbox")
.click(function(e) {
var el = $(this);
switch (el.data('checked')) {
// unchecked, going indeterminate
case 0:
el.data('checked', 1);
el.prop('indeterminate', true);
break;
// indeterminate, going checked
case 1:
el.data('checked', 2);
el.prop('indeterminate', false);
el.prop('checked', true);
break;
// checked, going unchecked
default:
el.data('checked', 0);
el.prop('indeterminate', false);
el.prop('checked', false);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label><input type="checkbox" name="checkbox" value="" checked id="checkbox"> Tri-State Checkbox </label>
由于我需要在表格中生成复选框,且不使用任何插件,因此我最终采用了以下解决方案:
<!DOCTYPE html>
<html>
<body>
Toto <input type="checkbox" id="myCheck1" onclick="updateChkBx(this)" /><br />
Tutu <input type="checkbox" id="myCheck2" onclick="updateChkBx(this)" /><br />
Tata <input type="checkbox" id="myCheck3" onclick="updateChkBx(this)" /><br />
Tete <input type="checkbox" id="myCheck4" onclick="updateChkBx(this)" /><br />
<script>
var chkBoxState = [];
function updateChkBx(src) {
var idx = Number(src.id.substring(7)); // 7 to bypass the "myCheck" part in each checkbox id
if(typeof chkBoxState[idx] == "undefined") chkBoxState[idx] = false; // make sure we can use stored state at first call
// the problem comes from a click on a checkbox both toggles checked attribute and turns inderminate attribute to false
if(chkBoxState[idx]) {
src.indeterminate = false;
src.checked = false;
chkBoxState[idx] = false;
}
else if (!src.checked) { // passing from checked to unchecked
src.indeterminate = true;
src.checked = true; // force considering we are in a checked state
chkBoxState[idx] = true;
}
}
// to know box state, just test indeterminate, and if not indeterminate, test checked
</script>
</body>
</html>
你需要使用JavaScript/CSS来模拟它。
在这里尝试一个例子:http://www.dynamicdrive.com/forums/archive/index.php/t-26322.html
HTML表单元素可以被禁用,这不是可以解决你的问题吗?你的用户会看到它有三种状态:选中、未选中和禁用。禁用状态是灰色的且不可点击。对我而言,这似乎类似于第三种状态中的“null”或“不适用”或任何你在寻找的东西。
在上述使用不定状态的答案的基础上,我想出了一个小技巧,可以处理单个复选框并使它们成为三态。
MVC Razor 每个复选框使用两个输入(复选框和带有相同名称的隐藏输入来始终强制提交值)。MVC 使用类似 "true" 的复选框值和与其同名的“false” 作为隐藏。这使得其适用于 API 调用中的布尔使用。此代码段使用第三个隐藏状态来在提交中保持最后一次请求的值。
使用下面初始化的复选框将会开始处于不定状态。勾选一次会打开复选框。再次勾选会关闭复选框(返回相同名称的隐藏值)。第三次选中会使其恢复到不定状态(并清除隐藏以便提交会产生空白)。
页面还使用其他隐藏 (例如 triBox2Orig) 填充了起始查询字符串上的任何值,因此可以在提交之间初始化和保持 3 种状态。
$(document).ready(function () {
var initCheckbox = function (chkBox)
{
var hidden = $('[name="' + $(chkBox).prop("name") + '"][type="hidden"]');
var hiddenOrig = $('[name="' + $(chkBox).prop("name") + 'Orig"][type="hidden"]').prop("value");
hidden.prop("origValue", hidden.prop("value"));
if (!chkBox.prop("checked") && !hiddenOrig) chkBox.prop("indeterminate", true);
if (chkBox.prop("indeterminate")) hidden.prop("value", null);
chkBox.change(checkBoxToggleFun);
}
var checkBoxToggleFun = function ()
{
var isChecked = $(this).prop('checked');
var hidden = $('[name="' + $(this).prop("name") + '"][type="hidden"]');
var thirdState = isChecked && hidden.prop("value") === hidden.prop("origValue");
if (thirdState) { // on 3rd click of a checkbox, set it back to indeterminate
$(this).prop("indeterminate", true);
$(this).prop('checked', false);
}
hidden.prop("value", thirdState ? null : hidden.prop("origValue"));
};
var chkBox = $('#triBox1');
initCheckbox(chkBox);
chkBox = $('#triBox2');
initCheckbox(chkBox);
});