注意:我已经尝试过在线解决我的问题的各种解决方案,但是没有一个起作用。
我正在尝试从表中传递内容,其中每一行都有自己的按钮来编辑该行中的内容。该按钮将打开Twitter Bootstrap下拉列表,该下拉列表具有两个按钮,一个是“编辑”按钮。编辑按钮将打开一个具有文本区域输入的模式。我希望文本区域在表中显示当前文本以供编辑。我将PHP与Symfony一起用于表单,将Twig用于页面呈现。
触发模式的按钮
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<button class="btn btn-sm dropdown-item edit-button" data-toggle="modal" data-target="#announcementEditModal" data-id="{{ announcement.id }}" data-content="{{ announcement.content }}" type="button">
<div class="announcement-actions">
<span class="fas fa-pencil-alt"></span> Edit announcement
</div>
</button>
模态
<div class="modal fade" id="announcementEditModal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header announcement-header">
<h5 class="modal-title" id="exampleModalLabel"><span class="fas fa-edit"></span> Edit new announcement</h5>
</div>
<div class="announcement-card-header card m-3 border-0">
<div class="announcement-card-header card-body border-0 p-1">
<span class="fa fa-info-circle fa-lg header-icon"></span>
<h5 class="align-header">ANNOUNCEMENT TEXT</h5>
</div>
<div class="announcement-card-body modal-body card border-0">
{{ form_start(editForm) }}
<div class="announcement-card-body">
<label for="exampleInputEmail1">ANNOUNCEMENT (SUPPORTS MARKDOWN)</label>
<textarea class="form-control" id="announcementText" rows="5" name="content"></textarea>
</div>
</div>
</div>
<div class="card-footer border-0 bg-white pt-0">
<div>
{{ form_widget(editForm.edit, {'left_icon': 'fas fa-check'}) }}
<button type="button" class="btn btn-light" data-dismiss="modal" aria-label="Close">
Cancel
</button>
</div>
</div>
{{ form_end(editForm) }}
</div>
</div>
</div>
JavaScript
<script type="text/javascript">
$(".edit-button").click(function(){
var content = $(this).data("content");
alert(content);
});
$('#announcementEditModal').on('shown.bs.modal', function () {
alert("modal open");
document.getElementById("#announcementText").val(content);
})
</script>
alert("modal open") does not fire.I have tried
'shown.bs.modal'and
'show.bs.modal'`。我正在使用Bootstrap 4.12
编辑:解决方案:将JS移到announces.js文件中后,在其中使用表单进行一些操作,触发器就起作用了。
答案 0 :(得分:1)
似乎您的HTML代码不完整或尚未与我们共享。
修正Javascript错误,因为您使用的是jQuery,所以使用它。
document.getElementById("#announcementText").val(content); // val is not function
替换为
document.getElementById("#announcementText").value = content;
$("#announcementText").val(content); // or jQuery way
这是小提琴:https://jsfiddle.net/7onyuw9f/1/
尝试2
下面是显示模块模式的示例,以显示如何避免全局变量以及更简洁。您可以了解有关此模式here
的更多信息 // Revealing Module Pattern
var MyProject = MyProject || {}; // Your global object
MyProject.Modal = function() { // Namespacing to Modal
var content = ""; // now content is local to this function;
var onEditClick = function() {
$(".edit-button").click(function() {
content = $(this).data("content");
alert(content);
});
};
var onModalShow = function() {
$('#announcementEditModal').on('shown.bs.modal', function() {
alert("modal open");
// $("#announcementText").val(content); <-- jQuery way
document.getElementById("announcementText").value = content;
});
};
var init = function() {
onEditClick();
onModalShow();
};
return {
init: init // expose this for other functions to call
}
}();
$(document).ready(function() {
MyProject.Modal.init();
});