jQuery模态弹出窗口未显示元素值

发布于 2025-01-29 02:43:08 字数 1249 浏览 1 评论 0原文

在ASP.NET Core 3.1剃须刀页面中打开Bootstrap 4.0模态后,JQuery show.bs.modal事件未显示模式中的元素值。但是,将分配的值记录到控制台显示正确。并使用show.bs.modal仍然不起作用。模态是否过早弹出?怎么了?

HTML模态:

<div id="eventModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title"><span id="eventTitle"></span></h4>
            </div>
            <div class="modal-body">
                <p id="pDetails"></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

jQuery:

$('#eventModal').modal('show').on('shown.bs.modal', function () {
    $('#eventModal #eventTitle').val("Sample Title");
    console.log($('#eventModal #eventTitle').val()); // This shows correctly.
}

After opening a Bootstrap 4.0 modal in an ASP.NET Core 3.1 Razor page, the jQuery shown.bs.modal event is not showing the element value in the modal. But, logging the assigned value to the console shows it correctly. And using show.bs.modal instead still doesn't work. Is the modal popping up prematurely? What could be wrong?

HTML Modal:

<div id="eventModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">×</button>
                <h4 class="modal-title"><span id="eventTitle"></span></h4>
            </div>
            <div class="modal-body">
                <p id="pDetails"></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

jQuery:

$('#eventModal').modal('show').on('shown.bs.modal', function () {
    $('#eventModal #eventTitle').val("Sample Title");
    console.log($('#eventModal #eventTitle').val()); // This shows correctly.
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱贱哒 2025-02-05 02:43:08

使用以下方法:

  1. .text() - 设置或返回所选元素的文本内容
  2. .html() - 设置或返回所选元素的内容(示例:包括:包括: html markup)
  3. .val() - 设置或返回表单字段的值

,因此您可以使用:

$('#eventModal').modal('show').on('shown.bs.modal', function () {
    $('#eventTitle').text("Sample Title");        
}

Using the following methods:

  1. .text() - Sets or returns the text content of selected elements
  2. .html() - Sets or returns the content of selected elements (example : including HTML markup)
  3. .val() - Sets or returns the value of form fields

So, you can use :

$('#eventModal').modal('show').on('shown.bs.modal', function () {
    $('#eventTitle').text("Sample Title");        
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文