我有一个模式,允许用户重复输入,以便他们可以从下拉菜单中添加用户并调整权限。
当前,我只能使用他们的选择来更新最新的下拉按钮。如果他们添加了三个用户并为第一个选择“ read”,则只会更新最新的条目。每个下拉文本都有不同的跨度。简化的代码在下面列出,但完整版本可以在here中看到。
<!-- Modal -->
<div class="modal fade new" id="new" tabindex="-1" role="dialog" aria-labelledby="new_modal" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="permissions">Permissions</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true"><i class="fas fa-times"></i></span>
</button>
</div>
<div class="modal-body">
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="users-tab" data-toggle="tab" href="#users" role="tab" aria-controls="users" aria-selected="true">Users</a>
</li>
<li class="nav-item">
<a class="nav-link" id="groups-tab" data-toggle="tab" href="#groups" role="tab" aria-controls="groups" aria-selected="false">Groups</a>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<!-- Content for the users tab -->
<div class="tab-pane fade show active" id="users" role="tabpanel" aria-labelledby="users-tab">
<div class="wrapper">
<div class="users">
<u><h6>Name</h6></u>
<span id="name"></span>
</div>
<div class="permissions">
<u><h6>Access Level</h6></u>
<span id="access_level"></span>
</div>
</div>
</div>
<!-- Content for the groups tab -->
<div class="tab-pane fade" id="groups" role="tabpanel" aria-labelledby="groups-tab">
Group Level Permissions
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" id="new_rule"><i class="fas fa-plus"></i></button>
<button type="button" class="btn btn-primary" data-dismiss="modal" data-toggle="modal" data-target="#saved" id="save">Save</button>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.js"integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function() {
var i = 1;
// When the user cicks the + icon, a new input and dropdown list will appear
$("#new_rule").click(function() {
// markup for the dropdown
var button = "<div class='btn-group'><button type='button' name='options' id='options' class='btn btn-danger dropdown-toggle' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'><span id='selected"+i+"'><span id='text'>Read</span></span></button><div class='dropdown-menu'><a class='dropdown-item 'href='#'>Read</a><a class='dropdown-item' href='#'>Write</a><a class='dropdown-item' href='#'>Full Control</a><div class='dropdown-divider'></div><a class='dropdown-item' href='#'>Learn More</a></div></div>"
// Markup for the name input
var input = "<input type='text' name='name' placeholder='name' class='form-control'>";
// Prepend the markup to the spans
$("#name").prepend(input);
$("#access_level").prepend(button);
// Update the dropdown to reflect what they selected *this is currently broken*
$('.dropdown-menu a').click(function(){
$('#text').text($(this).text());
})
// Increment the span ID so each dropdown has a different span.
i++
});
// When they click "Save", we gather their inputs into two separate JSON arrays for POST
$("#save").click(function() {
// Gather the names that were entered and dump them into a JSON array
var value = $("input[name='name']")
.map(function() {
return $(this).val();
}).get();
var jsonStr = JSON.stringify(value);
console.log(jsonStr);
$('#options').each(function(){
// Gather the access level assigned to those names and dump them into a JSON array.
var accessLevel = $("button[name='options']").map(function(){
return $(this).text();
}).get();
console.log(JSON.stringify(accessLevel));
});
});
});
</script>
答案 0 :(得分:0)
所以,您的问题出在(如您所说的)虚线上:
$('#text').text($(this).text());
这将找到ID为'text'的第一个元素,并使用您选择的文本设置该元素的文本。当然,一旦添加了多个规则,您将获得ID为“ text”的多个跨度(无论如何这不是一个好主意。请参见此处:https://stackoverflow.com/a/44009054/1178830)。
您可以给他们每个人一个自己的ID(例如,text1,text2 ...),也可以通过将上面的行替换为类似的内容来尝试查找没有ID的元素:
$(this).parent().siblings('button').find('span').text($(this).text());
这当然很大程度上取决于您在“按钮”变量中添加的元素的结构。