How to include a Form inside a Popover using Bootstrap 4.3?

五迷三道 提交于 2020-03-23 05:15:38

问题


I am trying to include a form inside a Popover, created using Bootstrap 4.3, Popper.js and jQuery 3. The latest version of each. The problem is that it works with text, it works with tags, but as soon as I use forms, labels and inputs it doesn't parse the HTML.

I have two examples in CodePen:

  1. Everything via data-attributes: link
  2. Via JS: link

Example 1 code

HTML:

<div class="container">
    <div class="row">
      <div class="col-12 mt-5">
        <h1>Bootstrap 4 form in popover</h1>
        <hr>
      </div>
    </div>
    <div class="row">
      <div class="col-12">
        <h2 class="mt-3">
          <span
            class="editable"
            data-toggle="popover"
            data-container="body"
            data-title="Edit"
            data-content="<form>
  <div class='form-group'>
    <label for='exampleInputEmail1'>Email address</label>
    <input type='email' class='form-control' id='exampleInputEmail1' aria-describedby='emailHelp' placeholder='Enter email'>
    <small id='emailHelp' class='form-text text-muted'>We'll never share your email with anyone else.</small>
  </div>
  <div class='form-group'>
    <label for='exampleInputPassword1'>Password</label>
    <input type='password' class='form-control' id='exampleInputPassword1' placeholder='Password'>
  </div>
  <div class='form-group form-check'>
    <input type='checkbox' class='form-check-input' id='exampleCheck1'>
    <label class='form-check-label' for='exampleCheck1'>Check me out</label>
  </div>
  <button type='submit' class='btn btn-primary'>Submit</button>
</form>">
            Modify this
          </span>
        </h2>
      </div>
    </div>
  </div>

CSS:

.editable {
  color: blue;
  border-bottom: 2px dashed blue;
}

JS:

$("h2 > .editable").popover({
  html: true
});

Example 2 code

HTML:

<div class="container">
    <div class="row">
      <div class="col-12 mt-5">
        <h1>Bootstrap 4 form in popover</h1>
        <hr>
      </div>
    </div>
    <div class="row">
      <div class="col-12">
        <h2 class="mt-3">
          <span
            class="editable"
            data-toggle="popover"
            data-container="body"
            data-title="Edit">
            Modify this
          </span>
        </h2>
        <div id="form-container" style="visibility: hidden">
          <form>
  <div class='form-group'>
    <label for='exampleInputEmail1'>Email address</label>
    <input type='email' class='form-control' id='exampleInputEmail1' aria-describedby='emailHelp' placeholder='Enter email'>
    <small id='emailHelp' class='form-text text-muted'>We'll never share your email with anyone else.</small>
  </div>
  <div class='form-group'>
    <label for='exampleInputPassword1'>Password</label>
    <input type='password' class='form-control' id='exampleInputPassword1' placeholder='Password'>
  </div>
  <div class='form-group form-check'>
    <input type='checkbox' class='form-check-input' id='exampleCheck1'>
    <label class='form-check-label' for='exampleCheck1'>Check me out</label>
  </div>
  <button type='submit' class='btn btn-primary'>Submit</button>
</form>
        </div>
      </div>
    </div>
  </div>

JS:

$("h2 > .editable").popover({
  content: function() {
    return $('#form-container').html();
  },
   html: true,
  placement: 'bottom'
});

回答1:


Setting:

sanitize: false

your popover form works. For details see docs

$("h2 > .editable").popover({
    html: true,
    sanitize: false
});
.editable {
  color: blue;
  border-bottom: 2px dashed blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>


<div class="container">
    <div class="row">
        <div class="col-12 mt-5">
            <h1>Bootstrap 4 form in popover</h1>
            <hr>
        </div>
    </div>
    <div class="row">
        <div class="col-12">
            <h2 class="mt-3">
          <span
                  class="editable"
                  data-toggle="popover"
                  data-container="body"
                  data-title="Edit"
                  data-content="<form>
  <div class='form-group'>
    <label for='exampleInputEmail1'>Email address</label>
    <input type='email' class='form-control' id='exampleInputEmail1' aria-describedby='emailHelp' placeholder='Enter email'>
    <small id='emailHelp' class='form-text text-muted'>We'll never share your email with anyone else.</small>
  </div>
  <div class='form-group'>
    <label for='exampleInputPassword1'>Password</label>
    <input type='password' class='form-control' id='exampleInputPassword1' placeholder='Password'>
  </div>
  <div class='form-group form-check'>
    <input type='checkbox' class='form-check-input' id='exampleCheck1'>
    <label class='form-check-label' for='exampleCheck1'>Check me out</label>
  </div>
  <button type='submit' class='btn btn-primary'>Submit</button>
</form>">
            Modify this
          </span>
            </h2>
        </div>
    </div>
</div>


来源:https://stackoverflow.com/questions/57413643/how-to-include-a-form-inside-a-popover-using-bootstrap-4-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!