Twitter bootstrap 引导模式上的Jquery验证工作不正常
我想把url表单页面(远程)加载到modal中,它工作得很好。但jquery对加载表单的验证只工作一次。再次加载表单时,除非刷新页面,否则不会执行jquery验证。为什么会这样。我的代码有什么问题Twitter bootstrap 引导模式上的Jquery验证工作不正常,twitter-bootstrap,jquery-validate,bootstrap-modal,Twitter Bootstrap,Jquery Validate,Bootstrap Modal,我想把url表单页面(远程)加载到modal中,它工作得很好。但jquery对加载表单的验证只工作一次。再次加载表单时,除非刷新页面,否则不会执行jquery验证。为什么会这样。我的代码有什么问题 <script type="text/javascript"> $(document).ready(function(){ $('[data-toggle="modal"]').click(function() { var url = $(this).attr('
<script type="text/javascript">
$(document).ready(function(){
$('[data-toggle="modal"]').click(function() {
var url = $(this).attr('href').replace(/\s/g,"+");
if (url.indexOf('#') == 0) {
$(url).modal('open');
}else {
$.get(url, function(data) {
$(data).modal();
}).success(function() {
$('input:text:visible:first').focus();
$("#frmAddMcb").validate({
submitHandler: function(form){
$('#modalAddMcb').modal('hide');
return postForm(form, "savemcb", "Body", "listmcb", false, false);
}
});
});
}
});
});
</script>
<div class="row-fluid">
<a href="addmcb" data-toggle="modal" class="btn btn-warning btn-xs link_href" title="Add MCB">Add MCB</a>
$(文档).ready(函数(){
$('[data toggle=“modal”]')。单击(函数(){
var url=$(this.attr('href').replace(/\s/g,“+”);
if(url.indexOf('#')==0){
$(url).modal('open');
}否则{
$.get(url、函数(数据){
$(data.modal();
}).success(函数(){
$('input:text:visible:first').focus();
$(“#frmAddMcb”).validate({
submitHandler:函数(表单){
$('modalAddMcb').modal('hide');
返回postForm(表单“savemcb”、“Body”、“listmcb”、false、false);
}
});
});
}
});
});
addmcb返回此表格
<div id="modalAddMcb" class="modal">
<div class="modal-dialogg">
<div class="modal-content">
<div class="modal-header">
<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
<h4 class="modal-title">Add MCB</h4>
</div>
<div class="modal-body">
<form id="frmAddMcb" enctype="multipart/form-data" class="form-horizontal" novalidate>
<div class="form-group">
<label class="col-md-2 col-sm-2 control-label">Name *</label>
<div class="col-md-9">
<input type="text" name="mcb.name" class="form-control" value='<s:property value="%{mcb.name}"/>' required>
</div>
</div>
<div class="form-group">
<label class="col-md-2 col-sm-2 control-label">Image *</label>
<div class="col-md-9">
<input type="file" name="fileUploaded" class="form-control">
</div>
</div>
<div class="modal-footer">
<button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
<button class="btn btn-primary" id="submit" type="submit"> Save</button>
</div>
</form>
</div>
</div>
</div>
×
添加微型断路器
名字*
形象*
取消
拯救
再次加载模式时,jquery验证不起作用,因为验证代码$(“#frmAddMcb”).validate({..})in document.ready()事件在需要验证表单时不会再次运行(因为页面已加载) 要解决此问题,您可以将验证代码添加到模态的加载事件中,以确保每次加载模态时它都会运行:
$('.modal').on('loaded.bs.modal', function (e) {
// your validation code
})
我也有类似的问题,但还没有解决方案。