2013-03-28 49 views
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 &lt; 0){ 
    count = 0; 
    } 
    document.getElementById('char_count').innerHTML = count ; 
} 

問題是某處大約在輸入文本框150個字符鍵入後,焦點轉移回inputtextbox的頂部。但是,遊標仍然如預期般結束。

我試過使用scrollTop位置和焦點,但沒有奏效。任何建議非常感謝。

這個問題只發生在IE8中,在forefox中工作正常。

+0

剛出於好奇,爲什麼在3個不同的事件中調用countChars函數?爲什麼不把它稱爲關鍵?你嘗試過一個嗎?行爲有什麼不同? – Revent 2013-03-28 06:44:01

+0

是的,我需要調用它的焦點和上/下鍵,因爲如果用戶試圖編輯現有的輸入文本,計數需要立即更新,並在每個字符輸入計數需要更新。我累了只打了一次,但沒有運氣。評論下面的線使它的工作,但我需要有這個。 document.getElementById('char_count')。innerHTML = count; – Vallikranth 2013-03-28 06:57:50

+0

順便說一句,不知道這行是否只是在你的問題的HTML編碼,但如果(計數< 0){可能是一個問題,如果你有JS的小於符號的HTML實體... – Revent 2013-03-28 07:30:37

回答

0

這可能不是你要找的答案,但這個劇本一直擔任我多年來:

的JavaScript:

/* This script and many more are available free online at 
The JavaScript Source!! http://javascript.internet.com 
Created by: Steve | http://jsmadeeasy.com/ 
http://www.javascriptsource.com/forms/character-counter.html */ 
function getObject(obj) { 
    var theObj; 
    if(document.all) { 
    if(typeof obj=="string") { 
    return document.all(obj); 
    } else { 
    return obj.style; 
    } 
} 
if(document.getElementById) { 
    if(typeof obj=="string") { 
    return document.getElementById(obj); 
    } else { 
    return obj.style; 
    } 
} 
return null; 
} 

function toCount(entrance,exit,text,characters) { 
var entranceObj=getObject(entrance); 
var exitObj=getObject(exit); 
var length=characters - entranceObj.value.length; 
if(length <= 0) { 
    length=0; 
    text='<span class="disable"> '+text+' </span>'; 
    entranceObj.value=entranceObj.value.substr(0,characters); 
} 
exitObj.innerHTML = text.replace("{CHAR}",length); 
} 

HTML & PHP:

(<span id="sBann">'.(255 - strlen($values['synopsis'])).' characters left</span>)<br /> 
<textarea name="synopsis" id="synopsis" rows="3" cols="70" maxlength="255" wrap="soft" onkeyup="toCount(\'synopsis\',\'sBann\',\'{CHAR} characters left\',255);">'.htmlentities($values['synopsis']).'</textarea>