tôi đi qua các cùng một vấn đề - tôi cần nó hiển thị: bật/tắt/bỏ qua
Dựa trên các biểu tượng vailable trong jquery ui() Tôi tạo ra đoạn mã sau (Tôi biết nó không phải là một plugin, nhưng wasnt rằng cần thiết trong trường hợp của tôi):
HTML tôi sử dụng là:
<input type="text" class="rotatestate" value="true"
data-state-style="cursor:pointer"
data-state-class="ui-icon"
data-state-values='[{"value":"true","class":"ui-icon-check","title":"title for true"},{"value":"false","class":"ui-icon-radio-off","title":"title for off"},{"value":"null","class":"ui-icon-cancel","title":"title for third state"}]'/>
Việc kiểm soát lấy json mảng trong dữ liệu nhà nước-giá trị cho bao nhiêu tiểu bang như bạn muốn xoay qua:
- giá trị: giá trị đó sẽ được nhập vào đầu vào
- lớp: lớp css (es) mà span mới sẽ có
- tiêu đề: tiêu đề bắt buộc mà sẽ được thiết lập trong khoảng thời gian tạo
Về cơ bản nó tạo ra một yếu tố <span class="data-state-class + classOfState" title="titleOfState">
và nhấp chuột cập nhật nó bằng cách cacling qua danh sách giá trị nhất định.
Tôi mã hóa nó để nó thậm chí cho phép thay đổi thông qua các phương tiện khác (tức là thiết lập giá trị của đầu vào trực tiếp) và cập nhật "điều khiển" khi $ ("đầu vào"). Change(); sự kiện được kích hoạt.
Mã jquery để xử lý nó:
/* rotatestate stontrol */
$("input.rotatestate", location).each(function(){
var states = $(this).attr("data-state-values");
var defaultClass = $(this).attr("data-state-class");
// no need to continue if there are no states
if(!states) {
return;
}
try {
states = JSON.parse(states);
} catch (ex) {
// do not need to continue if we cannot parse the states
return;
}
var stateControl = $("<span></span>");
if($(this).attr("data-state-style")) {
stateControl.attr("style", $(this).attr("data-state-style"));
}
stateControl.data("states", states);
stateControl.data("control", this);
stateControl.data("activeState", null);
$(this).data("control", stateControl);
if(defaultClass) {
stateControl.addClass(defaultClass);
}
// click on the control starts rotating
stateControl.click(function(){
var cState = $(this).data().activeState;
var cStates = $(this).data().states;
var control = $(this).data().control;
var newState = null;
if(cState != null) {
// go to the 'next' state
for(var i = 0; i < cStates.length; i++) {
if(cStates[i].value === cState.value) {
// last element
if(i === cStates.length - 1) {
newState = cStates[0];
} else {
newState = cStates[i+1];
}
break;
}
}
} else {
// no state yet - just set the first
newState = cStates[0];
}
$(control).attr("value", newState.value);
// trigger change
$(control).change();
});
// make sure to update state if the value is changed
$(this).change(function(){
var control = $($(this).data().control);
var cState = control.data().activeState;
var cStates = control.data().states;
if(cState != null) {
// remove "old state"
control.removeClass(cState['class']);
}
// add new State
var val = $(this).val();
$.each(cStates, function(){
if(this.value === val) {
control.data().activeState = this;
if(this.title) {
control.attr("title", this.title);
}
control.addClass(this['class']);
return false;
}
});
});
// trigger initial state
$(this).change();
$(this).after(stateControl);
$(this).hide();
});
Việc kiểm soát cũng là một phần của Form Controls của tôi Thư viện trên https://github.com/corinis/jsForm/wiki/Controls.
Trạng thái thứ ba, [FileNotFound] (http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx) là gì? – R0MANARMY
Ba trạng thái là null, true và false. – user336786
Có thể [this] (http://stackoverflow.com/questions/1726096/tri-state-check-box-in-html/1727191#1727191) câu hỏi sẽ đưa ra một số manh mối –