我有5個紡紗這幾乎完美的工作,只有一個小故障,我不能克服的。微調是不太正確的顯示號碼,如果微調是空白
什麼是假設發生的是,如果微調是空白的,不管前面的數字是在微調,它會顯示以前的號碼。
相反,它是做什麼的是,已在任一5個紡紗輸入了以前的任何數字,它顯示的是空白微調這個數字。
那麼如何才能得到它,使它顯示從微調對上號,如果微調是空白,而不是顯示來自任何微調的空白微調進入前面的最後一個號碼是多少?
下面是我的微調代碼:
function Spinner(elem,min, max){
this.elem = elem;
this.elem.value = min;
this.min = min;
this.max = max;
this.timer;
this.speed = 150; //milliseconds between scroll values
var selfO = this;
this.elem.onkeyup = function(){
var regex = /^[0-9]*$/;
if(!regex.test(selfO.elem.value)){
selfO.elem.value = selfO.elem.value.substring(0,selfO.elem.value.length-1);
return;
}
selfO.validateValue();
}
this.validateValue = function(){
if(Number(selfO.elem.value) > selfO.max) {selfO.elem.value = selfO.max;}
if(Number(selfO.elem.value) < selfO.min) {selfO.elem.value = selfO.min;}
}
this.stopSpinning = function(){
clearTimeout(selfO.timer);
}
this.spinValue = function(dir){
selfO.elem.value = Number(selfO.elem.value) + dir;
selfO.validateValue();
selfO.timer = setTimeout(function(){selfO.spinValue(dir);},selfO.speed);
}
};
window.onload=function(){
//create the Spinner objects
var SpinnerHours = new Spinner(document.getElementById('txtHours'),0,23);
var SpinnerMins = new Spinner(document.getElementById('txtMins'),0,59);
var SpinnerSecs = new Spinner(document.getElementById('txtSecs'),0,59);
var SpinnerWeight = new Spinner(document.getElementById('txtWeight'),0,100);
var SpinnerQuestion = new Spinner(document.getElementById('txtQuestion'),0,100);
document.getElementById('txtHours').onblur = function(){ this.value = cleanSpin(this.value);}
document.getElementById('txtMins').onblur = function(){ this.value = cleanSpin(this.value);}
document.getElementById('txtSecs').onblur = function(){ this.value = cleanSpin(this.value);}
document.getElementById('txtWeight').onblur = function(){ this.value = cleanSpin(this.value);}
document.getElementById('txtQuestion').onblur = function(){ this.value = cleanSpin(this.value);}
function cleanSpin(obj) {
if(obj > 0) {
var str = obj.replace(/^0*/,'');
lastSpin = str;
return str;
}
else {
return lastSpin;
}
}
var lastSpin = 1;
Spinner類從哪裏來?這不是標準的JavaScript AFAIK。 – GregL
我會猜測它來自jQuery UI,但我很好奇這行代碼。 cleanSpin(THIS.VALUE); –
我不想用代碼重載,但是如果你想要微調類,那麼現在我已經在包含值的驗證的問題中包含了這個。我認爲這個問題雖然是在我的cleanSpin()函數 – BruceyBandit