如何在 Ext.Message.Box 中使用键下事件



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

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 个字符的字符。

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

允许的最大输入字符数。

因此,您的代码将如下所示:

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

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

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>

我试过这个,它也起作用了。

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

最新更新