简体   繁体   English

在不支持bean的情况下即时获取inputText的值?

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

can I get the value of a inputTextare 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());
    }
}

In order to display the remaining chars in the dialog, I have to feed the method with current value in Textarea without saving the value to a backing bean! 为了在对话框中显示剩余的字符,我必须在Textarea中使用当前值来输入方法,而不必将值保存到备用bean! How can i get this value? 我如何获得该价值?

You can do this with a JavaScript method (no need to go to server). 您可以使用JavaScript方法执行此操作(无需转到服务器)。 Here is a sample: 这是一个示例:

<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>

You can get more details about keydown , keypress and keyup javascript functions here (in this case I preffer to validate the text on the keyup event). 您可以在此处获取有关keydownkeypresskeyup javascript函数的更多详细信息(在这种情况下,我倾向于验证keyup事件中的文本)。 Also you can modify the JavaScript function and send the id of your component instead of sending the whole component (this will make the JS available for inputText component and similars). 另外,您可以修改JavaScript函数并发送组件的ID,而不是发送整个组件(这将使JS可用于inputText组件和类似组件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM