【问题标题】:PrimeFaces input text enter keyPrimeFaces 输入文本输入键
【发布时间】:2017-06-04 08:07:33
【问题描述】:

JSF 2.0,PrimeFaces 5.3

您好,我开发了一个导航控件(使用 jquery),当您在表单的某些组件(输入文本、自动完成等)中按下回车键时,焦点会跳转到下一个组件,因此您无需使用即可浏览老鼠。我的组件之一是输入文本区域,因此我需要用其他替换回车键来换行。我研究并发现我可以使用下一个代码来捕捉输入文本区域中的回车键。

<p:inputText id="commentInput" rendered="#{foo.rendered}" 
value="#{foo.value}"
onkeypress="if (event.keyCode == 13) { onchange(); return false; }">
<f:ajax event="change" listener="#{foo.test}" />

我的问题是,是否可以使用其他组合键在输入文本区域中产生换行符?也许 Alt + Enter 键或类似的。 此应用程序是其他桌面应用程序的端口,因此我需要复制行为。

更新@DavidFlorez 提出的解决方案

onkeyup="if (event.ctrlKey &amp;&amp; event.keyCode == 13) {this.value = this.value.slice(0, $(this).caret().begin)+'\n'+this.value.slice($(this).caret()‌​.begin);}"

当我开始在 inputTextarea 中写入时,出现下一个错误:

Uncaught SyntaxError: missing ) after argument list

我检查了 ) 但我看到所有的都在关闭。我按 CTRL + Enter 键但没有任何反应。

【问题讨论】:

  • 这是一个输入文本,而不是您在文本中声明的“输入文本区域”。
  • InputTextArea 有 onkeypress 事件,示例不是我的代码。我的大问题是如何用其他组合键复制换行符。
  • 尝试使用event.shiftKey 在光标为if (event.keyCode == 13 &amp;&amp; event.shiftKey){ this.value = this.value.slice(0, $(this).caret().begin)+'\n'+this.value.slice($(this).caret().begin)}的位置添加文本中断
  • @DavidFlorez 我添加了您的代码,但如果 (event.ctrlKey && event.keyCode == 13) {this.value = this.value.slice(0, $( this).caret().begin)+'\n'+this.value.slice($(this).caret()‌​‌​.begin);}。抱歉,我不知道如何在评论中格式化代码
  • 有没有报错?

标签: jquery primefaces jsf-2


【解决方案1】:

使用@DavidFlorez 解决方案我修改了我的代码:

<p:inputTextarea id="input" immediate="#{cc.attrs.inmediate}" value="#{cc.attrs.bean[cc.attrs.property]}" 
                tabindex="#{groupOfControl.getNextIndex(cc)}" maxlength="#{cc.attrs.maxLength}" 
                disabled="#{cc.attrs.disabled}" binding="#{cc.attrs.binding}" onkeyup="keyOverTextArea(event, '#{cc.clientId}Var')" 
                required="#{cc.attrs.require}" styleClass="#{cc.attrs.styleClass}" 
           rows="#{cc.attrs.row}" cols="#{cc.attrs.cols}" autoResize="#{cc.attrs.autoResize}" widgetVar="#{cc.clientId}Var"
           style="width:#{cc.attrs.width}%;resize:none">
        <composite:insertChildren />      
     </p:inputTextarea>

onkeyup 属性调用我写过的 jquery 函数:

function keyOverTextArea(event, widgetVar){
    if(event.keyCode == 13 && event.ctrlKey){
        var component = $(document.getElementById(PF(widgetVar).id));
        component.val(component.val().slice(0, component.caret().begin)+'\n'+component.val().slice(component.caret().begin));
        return false;
    }

    if ((event.which == 9 && !event.shiftKey) || event.which == 13){
        var idNextComponent = findComponent.findIdNextComponent(PF(widgetVar).id, null, true);
        PrimeFaces.focus(idNextComponente);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 2011-01-07
    相关资源
    最近更新 更多