简体   繁体   English

自定义事件,用于检测数据标签中值的变化

[英]Custom event to detect change of value in data tag

I have a div with a form in it that has a data tag that i have named data-av 我有一个div,其中有一个表单,该表单中有一个我已将其命名为data-av的数据标签

I am appending the div as follows 我将div追加如下

> $("#desti_div").append("<div class='tmar"+count+"'><div
> class='form-group col-md-6 getspace'><label><i class='gicon fas
> fa-circle'></i> New Destination</label><input class='form-control
> input-lg form-field very_form' type='text' id='added_dest"+count+"'
> data-av='' name='added_dest' placeholder='New Destinaton' required><a
> href='' class='gored'><span
> class='form-control-feedback'></span>Delete</a></div><br/></div>");

I would like to fire an event every time the value of data-av changes. 我想在data-av的值每次更改时都触发一个事件。 The id of the input that has the data-av has the id id='added_dest"+count+"' as shown in the code above so i should target the input with that id. 如上代码所示,具有data-av的输入的ID为id id='added_dest"+count+"' ,因此我应使用该ID作为输入的目标。

I am thinking of this 我在想这个

$('#someID').data("key", "newValue").trigger('changeData'); 

$('#someID').on('changeData', function (e) {    
    alert('My Custom Event - Change Data Called! for ' + this.id);
});

How can i watch if newValue changes so that i am able to show the custom event. 如何查看newValue发生更改,以便能够显示自定义事件。

You can use Attribute selectors . 您可以使用Attribute selectors Also trigger the event after defining the changeData event handler function: 在定义changeData事件处理函数之后,还触发事件:

 $("#desti_div").append(`<div class='tmar"+count+"'><div class='form-group col-md-6 getspace'><label><i class='gicon fas fa-circle'></i> New Destination</label><input class='form-control input-lg form-field very_form' type='text' id='added_dest"+count+"' data-av='' name='added_dest' placeholder='New Destinaton' required><a href='' class='gored'><span class='form-control-feedback'></span>Delete</a></div><br/></div>`); $('[data-av]').on('changeData', function (e) { alert('My Custom Event - Change Data Called! for ' + this.id); }); $('[data-av]').data("av", "newValue").trigger('changeData'); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="desti_div"></div> 

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

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