简体   繁体   English

为什么我无法使用jQuery将HTML动态添加到页面?

[英]Why I'm not able to dynamically add the HTML to the page using jQuery?

I've following HTML code of Bootstrap Modal dialog box: 我正在遵循Bootstrap Modal对话框的HTML代码:

<div class="modal fade" id="rebateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Submit Form</h4>
      </div>
      <div class="modal-body">
        <p style="text-align: justify;"><span style="font-weight: 700;"></p>  
        <br/>
        <!-- Here I want to dynamically add the HTML from AJAX response -->
        <form id="request_form" method="post" class="form-horizontal" action="">
        </form>
      </div>
    </div>
  </div>
</div>

In the above code I want to add the HTML code dynamically after 在上面的代码中,我想在之后动态添加HTML代码

<div class="modal-body">
  <p style="text-align: justify;"><span style="font-weight: 700;"></p>  
  <br/>

using jQuery AJAX. 使用jQuery AJAX。

For it I tried below code: 为此,我尝试了以下代码:

$('#request_form').submit(function(e) {
  var form = $(this);

  var formdata = false;

  if(window.FormData) {
    formdata = new FormData(form[0]);
  }

  var formAction = form.attr('action');

  $.ajax({
    url         : 'xyz.php',
    type        : 'POST',    
    cache       : false,
    data        : formdata ? formdata : form.serialize(),
    contentType : false,
    processData : false,

    success: function(response) {alert(response);
        // Below variable contains the HTML code that I want to add after <br/>

        var htmlString = "<div class='alert alert-danger alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>"+response+"</div>"

        $(htmlString).insertBefore('div.modal-body:first-child');

    }
  });
  e.preventDefault();
});

But I'm not able to do so. 但是我不能这样做。 Getting error in console as 在控制台中出现错误

ReferenceError: htmlString is not defined

No new HTML is coming in Bootstrap modal. Bootstrap模式中没有新的HTML。

The variable response contains following string : 变量response包含以下字符串:

Id can't be blank<br>Please select Date<br>Image can't be blank<br>

Please help me in this regard. 请在这方面帮助我。

  • htrmlString is not htmlString . htrmlString 不是 htmlString

  • The selector div.modal-body:first-child will not match any element because div.modal-body is not the first-child of its container. 选择器div.modal-body:first-child将不匹配任何元素,因为div.modal-body不是其容器的第一个孩子。 As far as I can see, you only have one .modal-body then you can probably remove the :first-child pseudo selector. 据我所知,您只有一个.modal-body则可以删除:first-child伪选择器。

    Try this: $(htmlString).insertBefore('div.modal-body'); 试试这个: $(htmlString).insertBefore('div.modal-body'); .

As per your comment: 根据您的评论:

If you need to insert the content before the form in the modal, try this: 如果您需要在模式中的表单之前插入内容,请尝试以下操作:

$(htmlString).insertBefore('div.modal-body #request_form');

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

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