最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - e.preventDefault is not working while openning A modal after changing modal-text. modal is openning without changin

programmeradmin1浏览0评论

I am trying to change the modal-body text when modal trigger button is clicked . The modal is opening but the modal-body text is not changing. This is the code for modal:

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

Then source for preventing default event

$('.open-modal').click(function(e)
{
    e.preventDefault();
    alert('hello i am jquery');
    $('#myModal').modal('show');
});

Why alert('hello i am jquery'); line is not executing ?

I am trying to change the modal-body text when modal trigger button is clicked . The modal is opening but the modal-body text is not changing. This is the code for modal:

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

Then source for preventing default event

$('.open-modal').click(function(e)
{
    e.preventDefault();
    alert('hello i am jquery');
    $('#myModal').modal('show');
});

Why alert('hello i am jquery'); line is not executing ?

Share Improve this question edited May 25, 2023 at 11:19 wwkudu 2,7963 gold badges31 silver badges42 bronze badges asked Mar 27, 2017 at 13:05 avijitavijit 9212 gold badges15 silver badges22 bronze badges 2
  • When I run your code as a codepen, it works fine: codepen.io/anon/pen/PpdbrJ – mikeg542 Commented Mar 27, 2017 at 13:14
  • Yess . But this code is not working at mine pc . – avijit Commented Mar 27, 2017 at 13:18
Add a ment  | 

3 Answers 3

Reset to default 5

This may happen when your HTML takes precedence over jquery since your button specifes the target modal to open with the following attributes

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

Remove those and then the jquery will be fine

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

To trigger a modal to show programmatically, you can use the following:

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

You can also have an event handler being triggered and add your code in there:

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

In my case: Below code worked properly if the case fails:

$('#myModal').modal({show: 'false'});
发布评论

评论列表(0)

  1. 暂无评论