Javascript使附加和更新DOM变得复杂,因此可以使用remove()将其删除;
我有一个小问题,我正在尝试删除一个附加表行。每当用户单击.removeRow时,它就会激活函数removeCustomSetting()Javascript使附加和更新DOM变得复杂,因此可以使用remove()将其删除;,javascript,jquery,Javascript,Jquery,我有一个小问题,我正在尝试删除一个附加表行。每当用户单击.removeRow时,它就会激活函数removeCustomSetting() 单击该按钮后,不会删除表行。我认为这是因为append函数不更新DOM 我尝试了很多事情,比如更新DOM元素,但都没有成功 如果有人知道如何解决这个问题,我会非常感激:) 我们的桌子一排 <tbody id="customSettingsTableContent"> <tr id="settingRow_265"><td&
单击该按钮后,不会删除表行。我认为这是因为append函数不更新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>
在这里,我们在提交并获取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>");
}
);
}
}
函数addCustomSetting(inputId){
customSettingName=$('input[name=customSettingName]')。val();
如果(!customSettingName){alert(“不能为空”);}其他{
警报(“确定”+自定义设置名称+”);
$.post(“,{action:“addCustomSetting”,customSettingName:customSettingName,inputId:inputId},
功能(数据、状态){
var return\u addCustomSettingResponse=jQuery.parseJSON(数据);
警报(“成功”+数据+”);
$(“#customSettingsTableContent”).html(“+return\u addCustomSettingResponse.name+”“+return\u addCustomSettingResponse.date+”“+return\u addCustomSettingResponse.time+”“+return\u addCustomSettingResponse.inputId+””;
}
);
}
}
您忘了以示例html的方式提供表行ID
$("#customSettingsTableContent").html("<tr><td>" + return_a
还请注意,您可能希望使用.append,因为否则您在本节中的行数将不会超过一行。您忘了按照示例html的方式提供表行ID
$("#customSettingsTableContent").html("<tr><td>" + return_a
还请注意,您可能希望使用.append,因为否则,您在本节中的行数将不会超过一行。试试这个
函数addCustomSetting(){
$('.noRecords').remove();
var staticHtml='+$('#customSettingsTableContent tr')。长度+'new namenew last name';
$('#customSettingsTableContent').append(staticHtml);
}
函数removeCustomSetting(事件){
var t=$('#customSettingsTableContent tr').eq(0).find('th').length;
event.target.closest('tr').remove();//您只需要这行代码
如果($('#customSettingsTableContent tr')。长度==1){
$('#customSettingsTableContent')。追加('No More Records!')
}
}
a.fa{
宽度:2米;
高度:2米;
显示:内联块;
光标:指针;
背景:#eee;
文本对齐:居中;
线高:2;
}
a:悬停{
背景#FFC107;
}
身体{
填充:10px;
}
#
名字
姓
行动
1.
做记号
奥托
2.
雅各布
桑顿
3.
拉里
鸟
试试这个
函数addCustomSetting(){
$('.noRecords').remove();
var staticHtml='+$('#customSettingsTableContent tr')。长度+'new namenew last name';
$('#customSettingsTableContent').append(staticHtml);
}
函数removeCustomSetting(事件){
var t=$('#customSettingsTableContent tr').eq(0).find('th').length;
event.target.closest('tr').remove();//您只需要这行代码
如果($('#customSettingsTableContent tr')。长度==1){
$('#customSettingsTableContent')。追加('No More Records!')
}
}
a.fa{
宽度:2米;
高度:2米;
显示:内联块;
光标:指针;
背景:#eee;
文本对齐:居中;
线高:2;
}
a:悬停{
背景#FFC107;
}
身体{
填充:10px;
}
#
名字
姓
行动
1.
做记号
奥托
2.
雅各布
桑顿
3.
拉里
鸟
它是否存在于某个地方?确切地说,什么不起作用?单击按钮后,表行不会被删除。我认为这是因为append函数不更新DOM抱歉,没有说得更清楚。有一个实时演示,请登录:电子邮件:support@oasis-hosting.net password:oasishosting-通过单击管理转到其中一个服务,然后单击模块输入处的小加号,它就在那里。附加工作与删除某些内容有什么关系?我甚至不知道您在哪里使用了.append.it是否在某处活动?确切地说,什么不起作用?单击按钮后,表行不会被删除。我认为这是因为append函数不更新DOM抱歉,没有说得更清楚。有一个实时演示,请登录:电子邮件:support@oasis-hosting.net password:oasishosting-通过单击管理转到其中一个服务,然后单击模块输入处的小加号,它就在那里。附加工作与删除某些内容有什么关系?我甚至不知道你在哪里用过。
$("#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...);
}
});