【发布时间】:2013-09-23 12:29:56
【问题描述】:
我目前正在使用 Twitter Bootstrap 模态组件,我遇到了一个问题,我在使用 data-remote 属性远程加载的内容中的输入字段上使用 jquery 验证插件。
由于在 dom 中运行 jquery 验证后才加载内容,因此不会对新加载的元素进行验证。
我有一个解决方案,我修改 bootstrap.js(模态类),见下文。
var Modal = function (element, options) {
this.options = options
this.$element = $(element)
.delegate('[data-dismiss="modal"]', 'click.dismiss.modal', $.proxy(this.hide, this))
//this.options.remote && this.$element.find('.modal-body').load(this.options.remote)
this.options.remote && this.$element.find('.modal-body').load(this.options.remote, $.proxy(function () {
this.$element.trigger('loaded')
}, this)) //my additions
}
我触发加载外部 html 片段的调用的“加载”事件。
我已经连接了这个事件,我在新加载的元素上调用了验证。
$('#myModal').on('loaded', function () {
$.validator.unobtrusive.parse($(this));
});
我的问题是我必须修改 bootstrap.js 来实现这一点,有没有办法可以在不修改 bootstrap.js 的情况下让它在外部工作?有没有办法访问页面上的模态对象并将“加载”事件附加到它?我想在外部脚本或页面中执行此操作,因此我无需担心引导版本。
【问题讨论】:
-
在模态的
shown事件中进行验证是否有问题? -
@mccannf 对于远程内容,在加载新元素之前显示的触发器。
标签: javascript asp.net-mvc-4 twitter-bootstrap jquery-validate unobtrusive-validation