简体   繁体   English

onChange事件会影响整个表单

[英]onChange event affects to the entire form

There's a short Webix form, and I'm trying to highlight the field which value has changed, but I'm stuck with this. 有一个简短的Webix表单,我试图突出显示值已更改的字段,但我对此仍然坚持。

Here's my code: 这是我的代码:

    webix.ui({
      view:"form",   
      width: 250,
      elements: [  
        { view:"text", label:'Phone', labelPosition: "top", name:"phone", value: "" },   
        { view:"text", label:'Email', labelPosition: "top", name:"email", value:"" },
      ],
        on:{
          onChange: function(newv, oldv){           
            webix.html.addCss(this.getNode(), "new_v");         
          }
        }   
    });

The snippet: http://webix.com/snippet/b47894fa 摘要: http : //webix.com/snippet/b47894fa

As you can see, the onChange event affects to each field, no matter which of them have new values. 如您所见,无论哪个字段具有新值, onChange事件都会影响到每个字段。 Is there a way to fix this? 有没有办法解决这个问题?

You can access the source of onChange event by using this.$eventSource 您可以使用this.$eventSource访问onChange事件的源this.$eventSource

  onChange: function(newv, oldv){           
    var source = this.$eventSource;
    webix.html.addCss(source.getNode(), "new_v");           
  }

ValVert's idea of using individual event handlers is good, but you don't need to repeat the same event handling code in each view. ValVert使用单个事件处理程序的想法很好,但是您不需要在每个视图中重复相同的事件处理代码。 You can use the form's elementsConfig property to define the onChange handler once. 您可以使用表单的elementsConfig属性来一次定义onChange处理程序。 Here's your example, cleaned up . 这是您的示例,请整理 Type stuff in the form, and press Enter. 在表单中键入东东,然后按Enter。

 webix.ui({ view: 'form', width: 250, elementsConfig: { labelPosition: 'top', on: { onChange: function(newv, oldv) { webix.html.addCss(this.getNode(), 'new_v'); } } }, elements: [ { view: "text", label: 'Phone', name: 'phone' }, { view: "text", label: 'Email', name: 'email' }, ], }); 
 .new_v input { background-color: lightyellow; } 
 <script src="https://cdn.webix.com/edge/webix.js"></script> 

Add the event to each text and not to the form. 将事件添加到每个文本,而不是窗体。

webix.ui({
  view:"form",   
  width: 250,
  elements: [  
    { view:"text", label:'Phone', labelPosition: "top", name:"phone", value: "",
      on:
      {
          onChange: function(newv, oldv){           
          webix.html.addCss(this.getNode(), "new_v");         
        }
      }
    },   
    { view:"text", label:'Email', labelPosition: "top", name:"email", value:"" ,
      on:
      {
          onChange: function(newv, oldv){           
          webix.html.addCss(this.getNode(), "new_v");         
        }
      }
    },
  ]   
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM