【问题标题】:How to use keydown event in Ext.Message.Box如何在 Ext.Message.Box 中使用 keydown 事件
【发布时间】:2017-02-03 09:00:29
【问题描述】:

我想添加当我按下超过 250 个字符时触发的 keydown 事件。这是代码,

var c = Ext.MessageBox.show({
    title: "Version Remarks",
    id:'test',
    inputType: "textareafield",
    msg:"Please Enter Version Remarks: (Only 250 Character)",
    width: 375,
    buttons: Ext.MessageBox.OKCANCEL,
    multiline: true,
    label: Ext.MessageBox.label,
    fn: b,
    icon: Ext.MessageBox.INFO,
    modal: true,
    closable: false,
    allowBlank: false,

});

c.textArea.on('change', function (e, text, o) {

    if (text.length > 250) {
        c.msgButtons.ok.setDisabled(true);
        //c.label.setText("This is the label");
        alert("You are not able to enter more than 250 Character.!!")
    } else {
        c.msgButtons.ok.setDisabled(false);
    }
}

当我按下 251 个字符时,会显示弹出窗口,并允许我输入字符,但现在我想使用 onkeydown 事件,它不允许用户输入超过 250 个字符的任何字符。

【问题讨论】:

  • 您要通知用户还是不允许他进入?
  • 我要通知用户。

标签: javascript extjs sencha-touch


【解决方案1】:

使用文本框的maxLength 配置并调用setMaxLength 将其设置为250 个字符。
来自 sencha 文档。

允许的最大输入字符数。

所以你的代码看起来像:

var c = Ext.MessageBox.show({
     // your config
});
c.textArea.setMaxLength(250);

【讨论】:

    【解决方案2】:

    如果您不希望用户通知已达到最大字符限制并且不允许他输入更多字符,那么您可以使用textarea 元素的maxLength 属性(html 不是 extjs)来设置最大长度.

    c.textArea.el.dom.querySelector('textarea').maxLength=250;
    

    为了通知用户,我们需要使用keypress 事件来检查文本的长度,并在长度超过 250 时通知用户。

    工作示例

    Ext.application({
        launch : function() {
    var c = Ext.MessageBox.show({
        title: "Version Remarks",
        id:'test',
        inputType: "textareafield",
        msg:"Please Enter Version Remarks: (Only 250 Character)",
        width: 375,
        buttons: Ext.MessageBox.OKCANCEL,
        multiline: true,
        label: Ext.MessageBox.label,
        icon: Ext.MessageBox.INFO,
        modal: true,
        closable: false,
        allowBlank: false,
    
    });
    c.textArea.el.dom.querySelector('textarea').maxLength=250;
    c.textArea.el.dom.querySelector('textarea').onkeypress=function(){
    if(this.value.length==250){
     alert("You are not able to enter more than 250 Character.!!");
     return false;
    }  };
    
    }
    
    
    });
    <link rel="stylesheet" href="https://cdn.sencha.com/ext/gpl/4.1.1/resources/css/ext-all.css">
    <script type="text/javascript" src="https://cdn.sencha.com/ext/gpl/4.1.1/ext-all-debug.js"></script>

    【讨论】:

    • 好的,谢谢,但我需要通知用户您尝试输入超过 250 个字符,并且当用户单击“确定”按钮时,用户无法在 textarea 中键入任何内容。现在的问题是弹出窗口在输入 250 个字符后显示,但我点击弹出窗口的确定按钮,然后如果我输入任何字符而不是显示,然后再次弹出弹出窗口,所以我想限制用户在 250 个字符后输入。希望你能理解。
    • 为此,我们将使用按键事件来检查长度并限制并通知用户。查看我的更新答案
    【解决方案3】:

    我试过了,它也有效。

    var c = Ext.MessageBox.show({
            title: "Version Remarks",
            id: 'test',
            inputType: "textareafield",
            msg: "Please Enter Version Remarks: (Only 250 Character)",
            width: 375,
            buttons: Ext.MessageBox.OKCANCEL,
            multiline: true,
            label: Ext.MessageBox.label,
            fn: b,
            icon: Ext.MessageBox.INFO,
            modal: true,
            closable: false,
            allowBlank: false,
    
        });
    
        c.textField.maxLength = 250;
        c.textArea.el.dom.querySelector('textarea').onkeyup = function () {
            if (this.value.length > 250) {
                this.value = this.value.substr(0, 250);
                alert("Maximum 250 characters are allowed for version remark.");
                return false;
            }
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 2017-09-20
      • 1970-01-01
      • 2022-12-05
      相关资源
      最近更新 更多