Jquery 在ajax加载表单中隐藏DIV

Jquery 在ajax加载表单中隐藏DIV,jquery,ajax,Jquery,Ajax,我有一个包含动态生成表单的模态。它是通过ajax调用加载的: $(function() { $('[data-toggle="modal"]').click(function(e) { e.preventDefault(); var href = $(this).attr('href'); if (href.indexOf('#') == 0) { $(href).modal('open'); } else { $.get(href, fu

我有一个包含动态生成表单的模态。它是通过ajax调用加载的:

$(function() {
  $('[data-toggle="modal"]').click(function(e) {
    e.preventDefault();
    var href = $(this).attr('href');
    if (href.indexOf('#') == 0) {
      $(href).modal('open');
    } else {
    $.get(href, function(data) {
      $('<div class="modal fade" id="modalR" tabindex="-1" role="dialog" aria-labelledby="modalR" aria-hidden="true">' + data + '</div>').modal();
    });
    }
  });
});
}))


有什么提示吗?

可能是因为当块已经在文件中时,jQuery会检测到它并绑定事件。但是,当您通过ajax加载它时,这是在绑定之后,因此新块不会附加任何事件。所以把它放在你的页面上后,试着绑定它。我是说,像这样的事情:

$.get(href, function(data) {
    $('<div class="modal fade" id="modalR" tabindex="-1" role="dialog" aria-labelledby="modalR" aria-hidden="true">' + data + '</div>').modal();

    $('#location').hide();
    $("#type").change(function() {
        if (this.value == '12') {
            $('#location').show();
        } else {
            $('#location').hide();
        }
    }); 

});
$.get(href,函数(数据){
$(''+数据+'').modal();
$(“#位置”).hide();
$(“#type”).change(函数(){
如果(this.value==“12”){
$(“#位置”).show();
}否则{
$(“#位置”).hide();
}
}); 
});

我认为这不起作用,因为在调用ajax模式之前,元素的位置和类型都不存在。
$.get(href, function(data) {
    $('<div class="modal fade" id="modalR" tabindex="-1" role="dialog" aria-labelledby="modalR" aria-hidden="true">' + data + '</div>').modal();

    $('#location').hide();
    $("#type").change(function() {
        if (this.value == '12') {
            $('#location').show();
        } else {
            $('#location').hide();
        }
    }); 

});