如何获取 Mojo TextField 中文本的长度?
我正在尝试设置一个限制为 150 个字符的多行文本字段,我尝试使用计数器来执行此操作,但遇到了一个问题,即在删除文本时无法减少计数器,或者在粘贴时无法添加正确的数字文本,所以我的新方法是每次按下字母时获取文本的长度。
我已经尝试过这个:(在 textField
的 charsAllow
属性中调用)
if (this.controller.get("mensaje").mojo.getValue().length <= 150) {
return true;
}
this.controller.get("mensaje").mojo.blur();
return false;
但它不起作用......我调试了并且该函数在之后退出粗体行...它甚至不返回 true
或 false
。
我还尝试将长度值分配给变量或将文本分配给变量,然后获取长度,但什么也没有。
这是同样的问题。它在 getValue()
之后返回。
另外,也许由于这个问题,文本会滚动而不是换行,但是当 textField
失去焦点时,它会换行文本。
How do you get the length of the text inside a Mojo TextField?
I'm trying to set a multiLine TextField with a limit of 150 chars, I tried doing it with a counter, but ran into a issue of not being able to decrement the counter when the text was erased, or adding the right number when pasting text, so my new approach was to get the length of the text each time you press a letter.
I've already tried this: (gets called in the charsAllow
attribute of the textField
)
if (this.controller.get("mensaje").mojo.getValue().length <= 150) {
return true;
}
this.controller.get("mensaje").mojo.blur();
return false;
but it doesn't work.... I debugged and the function exits just after the line in bold... it doesn't even returns true
or false
.
I also tried assigning the length value to a variable or assigning the text to a variable and then get the length, but nothing.
It's the same issue. It returns just after the getValue()
.
Also, maybe because of this issue, the text scrolls instead of wrapping, but when the textField
loses focus it wraps the text.
发布评论
评论(4)
如果“mensaje”是文本字段的 HTML ID,请尝试获取它并使用 .innerHTML().length。换句话说,使用 Javascript/Prototype 函数而不是 Mojo 对象来处理 DOM 元素。
If 'mensaje' is the HTML id of your text field, try getting it and using .innerHTML().length. In other words, work with the DOM element using Javascript/Prototype functions instead of the Mojo object.
我的第一个猜测是“this”没有正确传递到 charsAllow 中。您是否 .bind(this) 作为参数传递的函数?
My first guess would be that "this" isn't being passed into charsAllow properly. Did you .bind(this) the function you're passing as an argument?
我发现这有点奇怪...函数 mojo.getValue() 实际上可以工作...但不是从“charsAllow”调用的函数内部...而且,由 charsAllow 调用的函数不能调用任何其他函数函数,它只是打破了函数不执行任何操作...有人有办法限制多行文本字段中的字符吗??? (mojo 文本字段,以保留外观:D)。谢谢!!
I found this a little odd... the function mojo.getValue() actually works... but not from inside the function called by "charsAllow"..., and also, the function called by charsAllow can't call any other function, it just breaks out of the function doing nothing... does someone have a way to limit the chars in a MultiLine TextField??? (mojo textfield, to preserve the look :D). Thanks!!
这个博客解释了一些关于文本字段的内容可能会有用:
http://kmdarshan.com/wordpress/?p=3305
this blog explains a bit about text fields might be useful:
http://kmdarshan.com/wordpress/?p=3305