mi sono imbattuto nel stesso problema - ne avevo bisogno per mostrare: abilitare/disabilitare/ignorare
Sulla base delle icone a vailable in jQuery UI() Ho creato seguente codice (So che la sua non è un plugin, ma che era necessario nel mio caso):
Il codice HTML che uso è:
<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"}]'/>
Il comando prende il JSON array in-stato-valori di dati per il maggior numero di stati come si desidera ruotare attraverso:
- valore: il valore che verrà immesso nell'input
- classe: classe CSS (es) che il nuovo arco sarà avere
- titolo: un titolo opzionale che verrà impostato nel giro creato
Si crea in pratica un elemento <span class="data-state-class + classOfState" title="titleOfState">
e fare clic su Aggiornamenti dal cacling attraverso il determinato elenco di valori.
L'ho codificato in modo tale da consentire anche il cambiamento tramite altri mezzi (ad esempio, impostare direttamente il valore dell'ingresso) e aggiorna il "controllo" quando $ ("input"). Change(); l'evento è attivato.
Il codice jQuery che gestisce:
/* 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();
});
Il controllo è anche parte del mio Form Controls Library https://github.com/corinis/jsForm/wiki/Controls.
Qual è il terzo stato, [FileNotFound] (http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx)? – R0MANARMY
I tre stati sono nulli, veri e falsi. – user336786
Forse [questo] (http://stackoverflow.com/questions/1726096/tri-state-check-box-in-html/1727191#1727191) la domanda darà alcuni indizi –