html form inside bootstrap popover not working

前端 未结 3 1668
小蘑菇
小蘑菇 2021-01-19 04:55

I\'m trying to use a form inside a bootstrap popover. Some basic html works (text styling, buttons), but the form does not. (neither do onclick() javascript actions)

相关标签:
3条回答
  • 2021-01-19 05:19

    You can use content callback. In any case you need to insert your html into an hidden element, not into the button:

    $('[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>

    0 讨论(0)
  • 2021-01-19 05:29

    Your HTML is just fine and works (see example below.) The issue is a new flag needs to be set to allow HTML in the popover for BS 4.3.1

    Per their release notes for that version is a breaking change:

    "Security: Fixed an XSS vulnerability (CVE-2019-8331) in our tooltip and popover plugins by implementing a new HTML sanitizer"

    See implementation of sanitize below in your example:

    $(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>

    0 讨论(0)
  • 2021-01-19 05:32

    If nothing works, try this

    $('#'+$(this).attr('aria-describedby')).find('.popover-body').html(myhtml);
    
    0 讨论(0)
提交回复
热议问题