将事件附加到动态呈现字段的更简洁的方式
cleaner way to attach event to dynamically rendered field
我为动态呈现的表单字段附加了一个更改事件:
var html ='';
html += '<div class="Gallery-overlayContentWrapper">';
html += '<center>';
html += '<h3>'+self.data('uploadHeading')+'</h3>'
html += parent;
html += '<form id="Gallery-uploadForm" enctype="multipart/form-data" method="post">';
html += '<input type="hidden" name="Gallery-Parent" id="Gallery-Parent" value="'+self.data('activeFolder')+'" />';
html += '<p><input type="file" name="file1" id="file1"></p>';
html += '<p><progress id="Gallery-uploadProgress" value="0" max="100"></progress></p>';
html += '</form>';
html += '</center>';
html += '</div>';
$(self).find(".Gallery-"+self.data('overlayType')).append(html);
$(self).find(".Gallery-"+self.data('overlayType')).fadeIn(); //This only covers the thumbs area
setTimeout(function(){
$(self).find('#file1').change(function(){
self.data('uploadFile')(self);
});
},1000);
这是可行的,但是像这样依赖于1秒后渲染的元素感觉有点粗糙,是否有更干净的方法来做到这一点?
不需要使用timeout,在附加
之后附加它 $(self).find(".Gallery-"+self.data('overlayType')).append(html);
$(self).find('#file1').change(function(){
self.data('uploadFile')(self);
});
$(self).find(".Gallery-"+self.data('overlayType')).fadeIn();
或使用委托
$(document).on('change', '#file1', function(){
self.data('uploadFile')(self);
});
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 而循环只设置php中输入字段中的第一个值
- 在输入字段中将最小金额设置为
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- JavaScript 检查隐藏字段中的值并将其设置为隐藏字段值
- 在wordpress一定时间后更改自定义字段
- 如何在HTML输入字段中添加不可删除的后缀
- Meteor-添加用户自定义字段的方法不起作用
- 互斥单选按钮和相应的输入字段
- 在IE9中的输入字段中输入焦点最近按钮
- 选中单选框时将属性添加到输入字段
- 带有验证和隐藏字段值的提交按钮
- 主干窗体隐藏字段未呈现
- 带有let的JS/EECMAScript6私有字段的模式
- 按钮字段确认点击不'不起作用
- jQuery Wan Spinner插件的多个字段
- 通过单击动态加载的表中同一行的另一个字段来更新一行的字段
- 简洁的方式获取所有输入字段的值
- 将事件附加到动态呈现字段的更简洁的方式