如何使用 Bootstrap 4.3 在 Popover 中包含表单?

Kev*_*aqi 2 jquery popover bootstrap-popover bootstrap-4

我正在尝试在Popover 中包含一个表单,该表单是使用 Bootstrap 4.3、Popper.js 和 jQuery 3 创建的。每个表单的最新版本。问题在于它适用于文本,也适用于标签,但是一旦我使用表单、标签和输入,它就不会解析 HTML。

我在 CodePen 中有两个例子:

  1. 一切都通过数据属性:链接
  2. 通过JS:链接

示例 1 代码

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>
Run Code Online (Sandbox Code Playgroud)

CSS:

.editable {
  color: blue;
  border-bottom: 2px dashed blue;
}
Run Code Online (Sandbox Code Playgroud)

JS:

$("h2 > .editable").popover({
  html: true
});
Run Code Online (Sandbox Code Playgroud)

示例 2 代码

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>
Run Code Online (Sandbox Code Playgroud)

JS:

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

gae*_*noM 9

环境:

sanitize: false
Run Code Online (Sandbox Code Playgroud)

您的弹出窗口有效。有关详细信息,请参阅文档

sanitize: false
Run Code Online (Sandbox Code Playgroud)
$("h2 > .editable").popover({
    html: true,
    sanitize: false
});
Run Code Online (Sandbox Code Playgroud)
.editable {
  color: blue;
  border-bottom: 2px dashed blue;
}
Run Code Online (Sandbox Code Playgroud)