2017-09-14 63 views
0

我有一個textarea,在輸入時會自動增長,如果更改了某個選項,則會有一些JS清除textarea。但是,當通過JS將值設置爲「」時,textarea不會縮回。關於如何讓這個工作的任何建議?將值設置爲「」後調整文本框的大小

function clearTextArea(){ 
 
\t document.getElementById('fldAdditionalInformation').value = ""; 
 
} 
 

 
function auto_grow(element) { 
 
    element.style.height = "0px"; 
 
    element.style.height = (element.scrollHeight+10)+"px"; 
 
}
<p>Change Value to clear Textbox: 
 
<select onchange="clearTextArea();"> 
 
<option>1</option> 
 
<option>2</option> 
 
<option>3</option> 
 
<option>4</option> 
 
</select> 
 
</p> 
 

 
<textarea style="width:100%;" id="fldAdditionalInformation" onkeyup="auto_grow(this)"></textarea>

回答

3

您清除文本區域後,您應該運行auto_grow

function clearTextArea(){ 
 
    var element = document.getElementById('fldAdditionalInformation'); 
 
    element.value = ""; 
 
    auto_grow(element) 
 
} 
 

 
function auto_grow(element) { 
 
    element.style.height = "0px"; 
 
    element.style.height = (element.scrollHeight+10)+"px"; 
 
}
<p>Change Value to clear Textbox: 
 
<select onchange="clearTextArea();"> 
 
<option>1</option> 
 
<option>2</option> 
 
<option>3</option> 
 
<option>4</option> 
 
</select> 
 
</p> 
 

 
<textarea style="width:100%;" id="fldAdditionalInformation" onkeyup="auto_grow(this)"></textarea>

+0

完美,非常感謝你:) – LordTaz