在 ASP.NET Core 3.1 Razor 頁面中打開 Bootstrap 4.0 模式后,jQueryshown.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">×</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.
}
uj5u.com熱心網友回復:
使用以下方法:
.text()- 設定或回傳選定元素的文本內容.html()- 設定或回傳選定元素的內容(例如:包括 HTML 標記).val()- 設定或回傳表單域的值
因此,您可以使用:
$('#eventModal').modal('show').on('shown.bs.modal', function () {
$('#eventTitle').text("Sample Title");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/475378.html
上一篇:將輸入值傳遞給asp-route
下一篇:使用ajax訪問模板的php變數
