e.preventDefault在更改模态文本后打开A模式时不起作用。 modal在不改变内容的情况下开放

时间:2017-03-27 13:05:39

标签: javascript jquery bootstrap-modal

我正在尝试在单击模态触发按钮时更改模态正文。但模态是开放的,但模态正文不会改变。 这是模态

的代码
var array1 = [{ description: "Virat Kohli", fq_id: 156 }, { description: "Lokesh Rahul", fq_id: 156 }, { description: "Chateshwar Pujara", fq_id: 156 }, { description: "Ajinke Rahane", fq_id: 156 }],
    array2 = [{ ans_description: "who is better?", fq_id: 156 }],
    map = new Map,
    result = array2.map(a => map.set(a.fq_id, {
        description: a.ans_description,
        options: []
    }).get(a.fq_id));

array1.forEach(a => map.has(a.fq_id) && map.get(a.fq_id).options.push(a.description));

console.log(result);

然后来源防止默认事件

<button type="button" class="btn btn-info open-modal" data-remote="false" data-toggle="modal"  data-target="#myModal">Register</button>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Modal Header</h4>
            </div>
            <div class="modal-body">
                <p>Some text in the modal.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>

    </div>
</div>

为什么要提醒(&#39;你好我是jquery&#39;);线路没有执行?

2 个答案:

答案 0 :(得分:2)

当您的HTML优先于jquery,因为您的按钮指定要使用以下属性打开的目标模态时,可能会发生这种情况

data-remote="false" data-toggle="modal"  data-target="#myModal"

删除那些,然后jquery就可以了

$('.open-modal').click(function(e)
{
    e.preventDefault();
    alert('hello i am jquery');
    $('#myModal').modal('show');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<button type="button" class="btn btn-info open-modal" >Register</button>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Modal Header</h4>
            </div>
            <div class="modal-body">
                <p>Some text in the modal.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>

    </div>
</div>

答案 1 :(得分:1)

要触发以编程方式显示的模式,您可以使用以下内容:

$('#myModal').modal('show');

您还可以触发事件处理程序并在其中添加代码:

$('#myModal').on('shown', function() {
    alert('hello i am jquery');
})