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

时间:2019-08-08 13:21:09

标签: jquery bootstrap-4 popover bootstrap-popover

我正在尝试在Popover中包含一个表单,该表单是使用Bootstrap 4.3,Popper.js和jQuery 3创建的。问题在于它可以处理文本,可以处理标签,但是一旦我使用表单,标签和输入,就无法解析HTML。

我在CodePen中有两个示例:

  1. 一切通过数据属性:link
  2. 通过JS:link

示例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>

CSS:

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

JS:

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

示例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>

JS:

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

1 个答案:

答案 0 :(得分:1)

设置:

sanitize: false

您的弹出窗口形式有效。有关详细信息,请参见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>