引导程序弹出窗口内的html表单不起作用

时间:2019-05-22 20:09:58

标签: jquery html twitter-bootstrap

我正在尝试在自举弹出窗口中使用表单。一些基本的html作品(文本样式,按钮),但形式却不行。 (也不执行onclick()JavaScript操作)

我尝试了几种方法,但无法在线找到任何解决方案。

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>

    <button type="button" class="btn btn-lg btn-danger" data-toggle="popover" data-html='true' data-content="
    <h3>This works</h3>
    <form>
    This does not:<br>
    <input type='text' name='firstname'><br>
    Last name:<br>
    <input type='text' name='lastname'>
    </form>
    ">popover with html form</button>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

    <script>
        $(function () {
            $('[data-toggle="popover"]').popover({
                html: true
            })
        })
    </script>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

您可以使用content callback。无论如何,您都需要将html插入隐藏元素而不是按钮中:

$('[data-toggle="popover"]').popover({
    html: true,
    content: function () {
        var content = $(this).attr("data-popover-content");
        return $(content).find(".popover-body").clone();
    }
})
<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>


<button type="button" class="btn btn-lg btn-danger" data-toggle="popover" data-html='true'
        data-popover-content="#yourContentHere">popover with html form
</button>

<div id="yourContentHere" style="display:none;">
    <div class="popover-body">
        <h3>This works</h3>

        <form>
            This does not:<br>
            <input type='text' name='firstname'><br>
            Last name:<br>
            <input type='text' name='lastname'>
        </form>
    </div>
</div>

答案 1 :(得分:1)

您的HTML很好并且可以正常工作(请参见下面的示例。)问题是需要设置一个新的标志,以允许BS 4.3.1的弹出窗口中包含HTML

根据该版本的发行说明,这是一个重大更改:

“安全性:通过实施新的HTML清理程序,修复了工具提示和弹出式插件中的XSS漏洞(CVE-2019-8331)”

在下面的示例中查看sanitize的实现:

$(function() {
  $('[data-toggle="popover"]').popover({
    html: true,
sanitize: false,
  })
})
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<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>




<button style="margin: 20px;" type="button" class="btn btn-lg btn-danger" data-toggle="popover" data-html='true' data-content="
    <h3>This works</h3>
    <form>
    This does too:<br>
    <input type='text' name='firstname'><br>
    Last name:<br>
    <input type='text' name='lastname'>
    </form>
    ">popover with html form</button>


<button style="margin: 20px;" type="button" class="btn btn-lg btn-info" data-toggle="popover" data-html='true' data-content="
    <h3>This works</h3>
    <form>
    This does duece:<br>
    <input type='text' name='firstname'><br>
    Last name:<br>
    <input type='text' name='lastname'>
    </form>
    ">Duece</button>

答案 2 :(得分:0)

如果没有任何效果,请尝试

$('#'+$(this).attr('aria-describedby')).find('.popover-body').html(myhtml);