【发布时间】:2020-11-24 08:26:06
【问题描述】:
我有一个创建新帖子的模式。我想让用户选择部门进行共享,所以我使用复选框来选择受众。
HTML:
<div class="modal fade" id="createNewPostModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Create New Post</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div class="container">
<form method="post" id="createNewPostForm">
<textarea rows="3" name="text" placeholder="Write something..."></textarea>
<div>
<p>Select audience to share</p>
<div>
<input type="checkbox" id="depACheckBox">
<label id="depACheckBoxLabel" for="depACheckBox"></label>
<input type="checkbox" id="depBCheckBox" >
<label id="depBCheckBoxLabel" for="depBCheckBox"></label>
<input type="checkbox" id="depCCheckBox">
<label id="depCCheckBoxLabel" for="CheckBox"></label>
</div>
</div>
<button type="submit" class="btn btn-success" onclick="return createNewPost(this.parentNode);" id="createNewPostButton" data-dismiss="modal">Share</button>
</form>
</div>
</div>
</div>
</div>
</div>
不同的用户有不同的部门要显示,并保存在 mongoDB 用户文档中。我需要在加载模式时设置复选框的标签。
我在页面加载时获取用户的文档,所以我在 getUser 函数中尝试:
$(document).ready(function() {
$("#createNewPostModal").on('load', function(){
document.getElementById('depACheckBoxLabel').innerText = window.user.depA.name;
document.getElementById('depBCheckBoxLabel').innerText = window.user.depB.name;
document.getElementById('depCCheckBoxLabel').innerText = window.user.depC.name;
});
});
我也尝试了innerHTML,但标签仍然为空。如何在模态显示时或之后设置标签文本?
【问题讨论】:
-
这些 id 是否在文档的其他任何地方使用?例如,您是否有此模式的多个实例?
-
我在很多页面中都需要这个,所以我把它放在顶部导航栏中。我正在使用 ejs 框架,导航栏填充了指向此模式的链接,通过 footer.ejs 文件中的一个函数进行搜索和注销,该函数检查用户是否登录。所以几乎所有页面都包含页脚和到模态的链接,但模态本身只存在于一个地方,在 footer.ejs 文件中。 id 不在其他任何地方。
标签: javascript html jquery node.js