extjs 如何通过类或标记选择器为多个元素设置值

6mzjoqzu  于 2023-11-18  发布在  其他
关注(0)|答案(2)|浏览(190)

在JQuery中,我可以使用class或tag选择器一次为input多个元素设置一个值:

  1. <form action="" id="mainForm">
  2. <input type="text" name="Phone" class="deftext" value="" >
  3. <input type="text" name="Number" class="deftext" value="" >
  4. <input type="text" name="Name" class="deftext" value="" >
  5. <input type="text" name="Lastname" class="deftext" value="" >
  6. <input type="text" name="Middlename" class="deftext" value="" >
  7. <input type="text" name="Age" class="deftext" value="" >
  8. <input type="text" name="Email" class="deftext" value="" >
  9. <input type="text" name="Occupation" class="deftext" value="" >
  10. </form>

个字符
我可以在ExtJS 4中做同样的事情,将下面的代码修改为单行吗?

  1. Ext.getCmp('mainForm').down('[name=Phone]').setValue('');
  2. Ext.getCmp('mainForm').down('[name=Number]').setValue('');
  3. Ext.getCmp('mainForm').down('[name=Name]').setValue('');
  4. ....

lnxxn5zx

lnxxn5zx1#

你的JQuery代码不是真正可读的,因为当你查看函数时,你看不到有多个字段受到影响。这就是为什么ExtJS不支持这样的语法。你可以使用

  1. Ext.getCmp('mainForm').down('[name=Phone]').setValue('');
  2. Ext.getCmp('mainForm').down('[name=Number]').setValue('');

字符串
或者使用

  1. Ext.getCmp('mainForm').getForm().setValues({
  2. Phone: '',
  3. Number: ''
  4. });


与您的JQuery示例不同,这两个类都是可读的,因为我不必知道哪些类应用于哪些字段。
如果要将所有字段重置为上次form.loadRecord操作期间设置的值,或者如果未执行form.loadRecord操作则重置为初始值,请使用form.reset

  1. Ext.getCmp('mainForm').getForm().reset();

展开查看全部
ghhaqwfi

ghhaqwfi2#

好吧,如果子组件有共同点(例如xtype),您可以这样做

  1. Ext.getCmp('mainForm').query('textfield').each(function(component){component.setValue('')});

字符串
但是不能,你不能在组件数组上使用组件方法。

相关问题