如何在 zk 中获取子字符串?

How to get substring in zk?

this例子:

<grid width="100%">
    <rows>
        <row>onChanging textbox: <textbox id="t1" onChanging="copy.value = event.value"/></row>
        <row>Instant copy: <textbox id="copy" readonly="true"/></row>
    </rows>
</grid>

我能否以某种方式只复制 event.value 的子字符串,比如前 4 个字符?我试过 onChanging="copy.value = $(substring(event.value, 0, 4)) 之类的东西,但语法不正确。

感谢您的帮助,不胜感激。

这是一个简单的任务,您只需要做这样的事情:

<grid width="100%">
    <rows>
        <row>onChanging textbox: <textbox id="t1" onChange='copy.setValue(self.getValue().substring(0,3))' instant="true"/></row>
        <row>instant copy: <textbox id="copy" readonly="true"/></row>
    </rows>
</grid>

但是,您必须小心,因为如果您在第一个文本框中的值小于您尝试获取的子字符串的长度时尝试执行子字符串,那么您将收到错误消息:

String a1 = "ho";
String a2 = a1.substring(0,3) //Here you will have an error...

所以我会建议你先做一个验证,然后再做子字符串......像这样:

<grid width="100%">
    <rows>
        <row>onChanging textbox: <textbox id="t1" onChange='copy.setValue(self.getValue().length() >= 3? self.getValue().substring(0,3):"")' instant="true"/></row>
        <row>instant copy: <textbox id="copy" readonly="true"/></row>
    </rows>
</grid>

检查验证 self.getValue().length() >= 3 中的数字 3 是否与我想要在子字符串 self.getValue().substring(0,3)

中获得的字母数量相同

我给你做了个例子:http://zkfiddle.org/sample/3afnseb/2-onChange-copy-textbox-example