0
我有一個jsf 2.0應用程序,其中我有一個輸入文本框和一個數字字符留在計算器底部的輸入文本框,我通過JavaScript更新。inputTextArea在IE8中失去焦點
<h:inputTextarea class="myInputTextClass"
value="#{bean.text}"
style="resize: none;width:445px;" type="text"
maxlength="255" cols="60" rows="3"
onfocus="countChars($('.myInputTextClass'),255)"
onkeyup="countChars($('.myInputTextClass'),255);"
onkeydown="countChars($('.myInputTextClass'),255);"
/>
<span id="char_count" style="font-style:italic;margin-left:-14px">255</span>
腳本:
function countChars(element, max) {
calculateCount(element.val().length, max);
if (element.val().length > max) {
element.val(element.val().substring(0, max));
}
}
function calculateCount(length, max){
var count = max - length;
if(count < 0){
count = 0;
}
document.getElementById('char_count').innerHTML = count ;
}
問題是某處大約在輸入文本框150個字符鍵入後,焦點轉移回inputtextbox的頂部。但是,遊標仍然如預期般結束。
我試過使用scrollTop位置和焦點,但沒有奏效。任何建議非常感謝。
這個問題只發生在IE8中,在forefox中工作正常。
剛出於好奇,爲什麼在3個不同的事件中調用countChars函數?爲什麼不把它稱爲關鍵?你嘗試過一個嗎?行爲有什麼不同? – Revent 2013-03-28 06:44:01
是的,我需要調用它的焦點和上/下鍵,因爲如果用戶試圖編輯現有的輸入文本,計數需要立即更新,並在每個字符輸入計數需要更新。我累了只打了一次,但沒有運氣。評論下面的線使它的工作,但我需要有這個。 document.getElementById('char_count')。innerHTML = count; – Vallikranth 2013-03-28 06:57:50
順便說一句,不知道這行是否只是在你的問題的HTML編碼,但如果(計數< 0){可能是一個問題,如果你有JS的小於符號的HTML實體... – Revent 2013-03-28 07:30:37