我正在尝试创建一个用户权限表,该表将根据复选框的选择显示分配用户的权限类型。
我尝试了几种我知道的方法,也进行了搜索,但每次似乎总是被困住。 我从这个答案jQuery append and remove values from checkbox to textarea得到了类似的代码,该代码有效。但是当我尝试在工作中实现它时,它似乎不起作用。
<table id="permissionsTable">
<thead>
<th>Name</th>
<th>Slug</th>
<th>Description</th>
</thead>
<tbody id="tableBody">
</tbody>
</table>
<div id="alloptions">
<ul>
<li>
<input type="checkbox" name="option1" value="Play" />Play</li>
<li>
<input type="checkbox" name="option1" value="Create" />Create</li>
<li>
<input type="checkbox" name="option1" value="Delete" />Delete</li>
</ul>
</div>
这是jQuery代码:
var checkboxes = $("input[type='checkbox']");
var permsTable = $("#permissionsTable");
var valuer = [];
checkboxes.on('change', function() {
var values = checkboxes.filter(':checked').map(function(x, y) {
return y.value;
}).get();
checkboxes.filter(':checked').each(function(a, b) {
valuer.push($(this).val());
});
if (checkboxes.is(':checked')) {
permsTable.find('tbody').detach();
permsTable.append($('<tbody>'));
$.each(valuer, function(c, d) {
//alert(x + " : " + y);
var nameCol = d.substr(0, 1).toUpperCase() + d.substr(1) + " " + "Game";
var slugCol = d.toLowerCase() + "-" + "Game";
var descriptionCol = "Allow user to " + d.toUpperCase() + " a " + "Game";
$("#permissionsTable").find('tbody').append("<tr><td>" + nameCol + "</td><td>" + slugCol + "</td><td>" + descriptionCol + "</td></tr>");
});
} else {
permsTable.find('tbody').detach();
permsTable.append($('<tbody>'));
btnGeneratePermissions.css("display", "none");
}
});
我希望在选中复选框时将复选框的值添加到表行中;然后取消选中就删除该行。
答案 0 :(得分:1)
使用Template Literal,这很容易
$("#alloptions").on("change", "[name=option1]", function(){
var mode = $(this).val();
const rowTemp = `<tr data-mode=${mode}>
<td><span style="text-transform: capitalize;">${mode} Game</span></td>
<td><span style="text-transform: lowercase;">${mode}</span>-Game</td>
<td>Allow user to <span style="text-transform: uppercase;">${mode}</span></td>
</tr>`;
if($(this).is(":checked")) {
$("#tableBody").append(rowTemp)
}else{
$("[data-mode='"+ mode +"']").remove();
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<table id="permissionsTable">
<thead>
<th>Name</th>
<th>Slug</th>
<th>Description</th>
</thead>
<tbody id="tableBody">
</tbody>
</table>
<div id="alloptions">
<ul>
<li>
<input type="checkbox" name="option1" value="Play" />Play</li>
<li>
<input type="checkbox" name="option1" value="Create" />Create</li>
<li>
<input type="checkbox" name="option1" value="Delete" />Delete</li>
</ul>
</div>
答案 1 :(得分:0)
您可以只使用append函数。 逻辑将很容易。
$(".chx").change(function() {
var row = document.createElement("tr");
if(this.checked) {
var v = $(this).val();
row.innerHTML = `<td>${v} Game</td>
<td>${v.toLowerCase()}-Game</td>
<td>Allow user to ${v.toUpperCase()} a Game</td>`;
row.setAttribute("name",$(this).val());
$('#tableBody').append(row);
}
else{
$("tr[name="+$(this).val()+"]").remove();
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<body>
<table id="permissionsTable">
<thead>
<th>Name</th>
<th>Slug</th>
<th>Description</th>
</thead>
<tbody id="tableBody">
</tbody>
</table>
<div id="alloptions">
<ul>
<li>
<input type="checkbox" value="Play" class="chx" />Play</li>
<li>
<input type="checkbox" value="Create" class="chx"/>Create</li>
<li>
<input type="checkbox" value="Delete" class="chx" />Delete</li>
</ul>
</div>
</body>
</html>