簡體   English   中英

在不支持bean的情況下即時獲取inputText的值?

[英]get value of inputText on the fly without backing bean?

我可以在不支持bean的情況下即時獲取inputTextare的值嗎?

<p:dialog>
<p:inputTextarea rows="5" cols="30" value="#{_var.note}" />
<h:outputText id="remaining" value="#{util.getCharactersRemaining("varibale of textarea", 160)} characters left" />



public class UtilFacade {
    public int getCharactersRemaining(String value, int maxLength) {
        log.info("length: " + value.length());
        return (maxLength - value.length());
    }
}

為了在對話框中顯示剩余的字符,我必須在Textarea中使用當前值來輸入方法,而不必將值保存到備用bean! 我如何獲得該價值?

您可以使用JavaScript方法執行此操作(無需轉到服務器)。 這是一個示例:

<script type="text/javascript">
    function getRemainingChars(textArea, maxLength) {
        var actualText = textArea.value;
        if (actualText.length > maxLength) {
            textArea.value = actualText.substring(0, maxLength);
        }
        var remainingChars = (maxLength > actualText.length) ? maxLength - actualText.length : 0;
        document.getElementById("frmMyPage:txtRemainingChars").value = remainingChars;
    }
</script>
<h:form id="frmMyPage">
<h:inputTextArea id="txtTextArea" cols="50" rows="10" onkeyup="getRemainingChars(this, 500);" />
<br />
<h:outputText id="txtRemainingChars" />
</h:form>

您可以在此處獲取有關keydownkeypresskeyup javascript函數的更多詳細信息(在這種情況下,我傾向於驗證keyup事件中的文本)。 另外,您可以修改JavaScript函數並發送組件的ID,而不是發送整個組件(這將使JS可用於inputText組件和類似組件)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM