Javascript 复杂的附加和更新 DOM,因此可以使用 remove(); 将其删除
Javascript complicated append and updating DOM so it able to be removed with remove();
我有一个小问题,我正在尝试删除附加的表行。每当用户单击 .removeRow 时,都会激活函数 removeCustomSetting()
点击该按钮后,表格行不会被删除。我认为这是因为追加函数不会更新 DOM。
我尝试了很多事情,例如更新 DOM 元素,但没有成功。
如果有人知道如何解决这个问题,我真的会很感激:)
我们的表格行
<tbody id="customSettingsTableContent">
<tr id="settingRow_265"><td>ertert</td><td>17-03-2016 19:19:59</td><td><a style="cursor:pointer;" class="removeRow" onclick="removeCustomSetting('265','p3rstuxgq7');"><li class="fa fa-close"></li></a></td></tr>
</tbody>
在这里,我们按 id 删除行(就像表中的这个settingRow_265 )
一样。每个 ID 在表中都是唯一的,因为它是从数据库中的自动增量中提取的。
function removeCustomSetting(settingId, inputId) {
$.post("", { action: "removeCustomSetting", settingId: settingId, inputId: inputId },
function(data,status){
if(status=='success') {
$("#settingRow_" + settingId + "").remove();
var countCustomSettingRow = $('#customSettingsTable tr').length-1;
if(countCustomSettingRow<1) { $("#customSettingsTableContent").html("<tr><td>No custom settings found...</td><td></td><td></td></tr>"); }
}
}
);
}
在这里,我们在提交并获取 JSON 数据后将行添加到表中。
function addCustomSetting(inputId) {
customSettingName = $('input[name=customSettingName]').val();
if(!customSettingName) { alert("Can't be empty"); } else {
alert("OK "+ customSettingName +"");
$.post("", { action: "addCustomSetting", customSettingName: customSettingName, inputId: inputId },
function(data,status){
var return_addCustomSettingResponse = jQuery.parseJSON(data);
alert("success " + data + "");
$("#customSettingsTableContent").html("<tr><td>" + return_addCustomSettingResponse.name + "</td><td>" + return_addCustomSettingResponse.date + " " + return_addCustomSettingResponse.time + "</td><td>" + return_addCustomSettingResponse.inputId + "<a style='"cursor:pointer;'" onclick='"removeCustomSetting('" + return_addCustomSettingResponse.id + "','" + return_addCustomSettingResponse.inputId + "');'"><li class='"fa fa-close'"></li></a></td></tr>");
}
);
}
}
您忘记以示例 html 的方式为表行提供 ID。
$("#customSettingsTableContent").html("<tr><td>" + return_a
添加 id 后,它应该再次开始工作。但是,如果您只是停止使用 onclick 属性,那会更容易。为锚标记提供一类 delete-custom-setting
,然后使用它(仅一次,不在事件处理程序或函数中。此外,删除 onclick 属性。
$("#customSettingsTableContent").on('click', '.delete-custom-setting', function () {
var thisRow = $(this).closest('tr');
var siblings = thisRow.siblings();
thisRow.remove();
if (siblings.length === 0) {
$("#customSettingsTableContent").html(...no more content etc...);
}
});
另请注意,您可能希望使用 .append,否则此部分中的行永远不会超过一行。
试试这个,
function addCustomSetting() {
$('.noRecords').remove();
var staticHtml = '<tr> <th scope="row">' + $('#customSettingsTableContent tr').length + '</th> <td>new name</td><td>new last name</td><td> <a href="#" onclick="removeCustomSetting(event)"> <div class="fa fa-close"></div></a> </td></tr>';
$('#customSettingsTableContent').append(staticHtml);
}
function removeCustomSetting(event) {
var t = $('#customSettingsTableContent tr').eq(0).find('th').length;
event.target.closest('tr').remove(); // you will only need this line of code
if ($('#customSettingsTableContent tr').length == 1) {
$('#customSettingsTableContent').append('<tr class="noRecords"> <td colspan="' + t + '">No More Records!</td></tr>')
}
}
a .fa {
width: 2em;
height: 2em;
display: inline-block;
cursor: pointer;
background: #eee;
text-align: center;
line-height: 2;
}
a:hover .fa {
background: #FFC107;
}
body {
padding: 10px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<button type="button" onclick="addCustomSetting()" class="btn btn-primary">
<div class="fa fa-plus"></div>
</button>
<table id="customSettingsTableContent" class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>actions</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>
<a href="#" onclick="removeCustomSetting(event)">
<div class="fa fa-close"></div>
</a>
</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>
<a href="#" onclick="removeCustomSetting(event)">
<div class="fa fa-close"></div>
</a>
</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>
<a href="#" onclick="removeCustomSetting(event)">
<div class="fa fa-close"></div>
</a>
</td>
</tr>
</tbody>
</table>
希望这有帮助..:)
相关文章:
- 我可以使此幻灯片图像自动播放吗?
- HTML/JavaScript拖动&Drop-是否可以使重影图像*NOT*看起来'冲洗掉'
- 可以使 jquery 点击事件在所有其他点击事件之后执行
- 是否有一个Javascript函数可以使代码在运行时延迟
- 有什么方法可以使页面无法刷新吗
- 使用chrome.downloads.download,是否可以使文件不弹出下载栏
- javascript是否有一个选项可以使句点与任何字符匹配,包括换行符
- 有一种方法可以使jslint在使用控制台时显示警告或错误
- 有没有任何方法可以使<选择>转到url
- 有没有任何方法可以使这个名称生成循环运行最短的秒数
- JSXGraph 中有什么方法可以使图像不透明
- 有没有一种方法可以使Iframe根据需要进行扩展
- 是否可以使find()和findOne()方法只返回模式字段
- Javascript.创建类似数组的对象.是否可以使 .length 自动增量
- 是否可以使多个引导弹出窗口更加干燥
- 是否可以使控制器返回模式窗口的数据?MVC5 ASP.NET
- Jquery脚本是否可以使更小或更高效
- 有哪些方法可以使客户端更难操作 DOM
- 我们可以使数据列表自动完成以搜索文本和值吗?
- 是否有JS功能可以使Brightcove智能播放器视频开始播放