我是Javascript的初學者,這是我的第一個不僅僅是「剪切/粘貼/破解」的Javascript。我創建了一個計算器,用於輸入輸入時更新輸出,當輸入框模糊然後聚焦時,可以清除所有「答案盒」,但如果我將輸入框中的值退回,「答案盒」仍會顯示基於最後一個字符的'答案'。被退後的價值。當值背對背時清除其他文本框的問題
在我的'validiateTheInput'功能。我可以聲明一個'if ='3''來清除它們,當'3'是值時(最終不起作用),它會起作用,但如果字段出現,我似乎無法捕捉它空白做用戶退出框中的值。
我是不是癡迷於某種愚蠢的東西,還是我只是想念一些東西?
繼承人整個事情(與一些基本的HTML ommitted): 驗證功能也有點矯枉過正,因爲我正在試圖趕上'空白輸入'做退格。
//jQuery keyup to grab input
$(document).ready(function() {
$('#totalFeet').keyup(function() {
validiateTheInput();
});
});
//clear calculated values
function clearBoxes(answerbox, answerbox1, answerbox2, totalFeetField) {
answerbox.value = "";
answerbox1.value = "";
answerbox2.value = "";
totalFeetField.value = "";
};
//validate input, then go to callAll (calc the output and display it)
function validiateTheInput() {
var totalFeetField = document.getElementById('totalFeet');
var answerbox = document.getElementById('answerbox').value;
var answerbox1 = document.getElementById('answerbox1').value;
var answerbox2 = document.getElementById('answerbox2').value;
// feel like I should be able to catch it here with the length prop.
if (totalFeetField.value.length == 0) {
clearBoxes(answerbox, answerbox1, answerbox2, totalFeetField);
}
// if input is usable, do the good stuff...
if (totalFeetField.value != "" && !isNaN(totalFeetField.value)) {
callAll(); // call the function that calcs the boxes, etc.
}
// if input is NaN then alert and clear boxes (clears because a convenient blur event happens)
else if (isNaN(totalFeetField.value)) {
alert("The Total Sq. Footage Value must be a number!")
document.getElementById('totalFeet').value = "";
}
// clears the input box (I wish) if you backspace the val. to nothing
else if (totalFeetField.value == '3') {
clearBoxes(answerbox, answerbox1, answerbox2, totalFeetField);
}
// extra effort trying to catch that empty box :(
else if (typeof totalFeetField.value == 'undefined' || totalFeetField.value === null || totalFeetField.value === '') clearBoxes(answerbox, answerbox1, answerbox2, totalFeetField);
}
//group all box calc functions for easy inline call
function callAll() {
calcFirstBox();
calcSecondBox();
calcThirdBox();
}
// calculate box fields based on input box
function calcFirstBox() {
var totalFeetField = document.getElementById('totalFeet');
var answer = totalFeetField.value * 5.95; // set multiplier
document.getElementById('answerbox').value = answer.toFixed(2);
}
// calc the second box
function calcSecondBox() {
var totalFeetField = document.getElementById('totalFeet');
var answer = totalFeetField.value * 18.95; // set multiplier
document.getElementById('answerbox1').value = answer.toFixed(2);
}
// calc the third box
function calcThirdBox() {
var totalFeetField = document.getElementById('totalFeet');
var answer = totalFeetField.value * 25.95; // set multiplier
document.getElementById('answerbox2').value = answer.toFixed(2);
}
HTML:
<div id="calculator">
<form name="calculate">
<label for="total">Total Value to Calculate:</label>     
<input id="totalFeet" type="text" name="total" size="15" onfocus="clearBoxes(totalFeet, answerbox, answerbox1, answerbox2);"><br /><br />
<label for="answerbox">Total Value X $5.95:    $</label>
<input id="answerbox" onfocus="this.blur();" type="text" name="answerbox" size="15"><br /><br />
<label for="answerbox1">Total Value X $18.95:   $</label>
<input id="answerbox1" onfocus="this.blur();" type="text" name="answerbox1" size="15"><br /><br />
<label for="answerbox2">Total Value X $25.95:   $</label>
<input id="answerbox2" onfocus="this.blur();" type="text" name="answerbox2" size="15">
</form>
</div>
非常感謝!我也會讀你的帖子,因爲我正在尋找各種增強密碼的方法。我的下一步是/應該在輸出框上放一些淡入淡出的效果,以便在外觀上更加平滑:) – rhaag71 2011-01-29 21:13:24
@rhaag:我忘了提及,還有一些jQuery插件,後來幾個帖子規範化了輸入`事件我在Internet Explorer的鏈接文章中討論。祝你好運:-) – 2011-01-29 21:15:49
博客文章非常棒!這完全解釋了爲什麼(今天早上),當我試圖將fadeIn添加到不工作的輸出中時,並且在嘗試使用fadeOut(作爲調試動作)時,它會在錯誤的時刻淡出(它也會消失出整個文本框...錯誤的選擇器!)。你的博客讓我記得有關keyUp/keyDown的不同之處(keyUp是我嘗試淡入淡出的地方)。非常感謝你! – rhaag71 2011-01-29 21:35:32