我在我的應用程序中有多個錶行並想要刪除其中的一個。 我有一個功能,當我點擊一個按鈕時,另一個錶行。我想檢查一個元素是否有其他元素內的特定類名
首先我:
<table>
<thead> ... </thead>
<tbody>
<tr id='firstObject'> <td> <i class="icon-chevron-right clickable expand-alert"></i> </td> <td> @Ajax.RawActionLink("<i class='icon-trash'></i>", "_DeleteAlert", "Group", new { alertId = alert.Id }, new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "onDeleteAlertSuccess(" + alert.Id + ")"
}, new { @title = "Verwijder alarm" }) </td> </tr>
<tr id='secondObject'> <td> <i class="icon-chevron-right clickable expand-alert"></i> </td> <td> @Ajax.RawActionLink("<i class='icon-trash'></i>", "_DeleteAlert", "Group", new { alertId = alert.Id }, new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "onDeleteAlertSuccess(" + alert.Id + ")"
}, new { @title = "Verwijder alarm" }) </td> </tr> ....
但是,如果我在i
點擊類icon-chevron-right
變得icon-chevron-down
和獲取:
<tbody>
<tr id='firstObject'> <td> <i class="icon-chevron-down clickable expand-alert"></i> </td> <td> @Ajax.RawActionLink("<i class='icon-trash'></i>", "_DeleteAlert", "Group", new { alertId = alert.Id }, new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "onDeleteAlertSuccess(" + alert.Id + ")"
}, new { @title = "Verwijder alarm" }) </td> </tr>
<tr id='info'> ... </tr>
<tr id='secondObject'> <td> <i class="icon-chevron-right clickable expand-alert"></i> </td> <td> @Ajax.RawActionLink("<i class='icon-trash'></i>", "_DeleteAlert", "Group", new { alertId = alert.Id }, new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "onDeleteAlertSuccess(" + alert.Id + ")"
}, new { @title = "Verwijder alarm" }) </td> </tr> ....
沒有談到的伎倆。如果我點擊刪除按鈕,我想刪除該行,並且如果$(i).hasClass('icon-chevron-down')
也刪除與id='info'
的行的下一行。我的代碼tr的沒有id,這只是爲了更好地解釋它。
這裏是我的功能:
function onDeleteAlertSuccess(id) {
$('#alert-detail-table').find('tr').each(function() {
if ($(this).attr('data-id') == id) {
$(this).fadeOut(400, function() {
if ($(this).next($('td').next($('i').hasClass('icon-chevron-down')))) {
$(this).next('tr').remove();
}
$(this).remove();
});
}
});
}
第二,如果始終是真實的,但它必須是如果!$('i').hasClass('icon-chevron-down')
假的。現在,如果id='icon-chevron-right'
刪除了下一個表格行id='secondObject'
,那就不對了。
我該如何解決這個問題?
編輯:
我解決了這個問題,幫助傑里米:
function onDeleteAlertSuccess(id) {
$('#alert-detail-table').find('tr').each(function() {
if ($(this).attr('data-id') == id) {
$(this).fadeOut(400, function() {
var tr = $(this);
var nextElement = tr.next();
tr.find("i").each(function() {
if ($(this).hasClass("icon-chevron-down")) {
nextElement.remove();
}
});
//if ($(this).first($('td:first-child').hasClass('icon-chevron-down'))) {
// debugger;
//}
//if ($(this).next($('td').next($('i').hasClass('icon-chevron-down')))) {
// $(this).next('tr').remove();
//}
$(this).remove();
});
}
});
}
您的HTML和JS似乎沒有關係。 – Raptor
你的問題缺乏清晰度,你的代碼在解釋我們的情況方面做的很少。 –
對不起@ShivanRaptor我編輯了我的問題。他們被鏈接;) – Jeffrey