gpt4 book ai didi

javascript - 如何在 jQuery 中删除选定的消息(带有复选框)?

转载 作者:太空宇宙 更新时间:2023-11-03 10:23:58 25 4
gpt4 key购买 nike

我正在制作一个消息系统,它有很多 AJAX。我正在尝试添加带有复选框的批量操作功能。我已经添加了复选框,但我的问题是我不知道如何对所选消息进行处理。

这是我在单击复选框时发生的函数:

function checkIt(id) {
if ($('#checkbox_' + id).is(':checked')) {
$('#' + id).addClass("selected");
}
else {
$('#' + id).removeClass("selected");
}
}

但是,我不知道从那里去哪里。

enter image description here

这里是消息列表中[由 PHP 生成的] 行之一的一些示例标记:

<div class="line" id="33" >

<span class="inbox_check_holder">
<input type="checkbox" name="checkbox_33" onclick="checkIt(33)" id="checkbox_33" class="inbox_check" />
<span class="star_clicker" id="star_33" onclick="addStar(33)" title="Not starred">
<img id="starimg_33" class="not_starred" src="images/blank.gif">
</span>
</span>
<div class="line_inner" style="display: inline-block;" onclick="readMessage(33, 'Test')">
<span class="inbox_from">Nathan</span>
<span class="inbox_subject" id="subject_33">Test</span>
<span class="inbox_time" id="time_33" title="">[Time sent]</span>
</div>

</div>

如您所见,每一行都将 id 属性设置为实际的消息 ID。

在我上面的函数中,您可以看到我是如何检查它的。但是,现在我需要做的是在单击“删除”按钮时发送 AJAX 请求以删除所有选定的消息。

这是我目前拥有的删除按钮:

$('#delete').click(function() {
if($('.inbox_check').is(':checked')) {

}
else {
alertBox('No messages selected.'); //this is a custom function
}
});

我还将制作批量标记为已读、标记为未读、移除星标添加星标按钮,所以一旦我知道如何使这个批量删除工作,我可以使用相同的方法来做这些其他事情。

对于 PHP 部分,我如何删除所有通过 mysql_query 在 AJAX 请求中发送的请求?我知道它必须与数组有关,但我只是不知道执行此操作的代码。

提前致谢!

最佳答案

这个怎么样

$('#delete').click(function() {
var checked = $('.inbox_check:checked');
var ids = checked.map(function() {
return this.value; // why not store the message id in the value?
}).get().join(",");
if (ids) {
$.post(deleteUrl, {idsToDelete:ids}, function() {
checked.closest(".line").remove();
});
}
else {
alertBox('No messages selected.'); // this is a custom function
}
});

编辑:正如旁注,您不需要生成那些增量 ID。您可以消除大量的字符串解析并改用 jQuery。首先,将消息 ID 存储在复选框的值中。然后,在给定行的任何点击处理程序中:

var line = $(this).closest(".line");  // the current line
var isSelected = line.has(":checked"); // true if the checkbox is checked
var msgId = line.find(":checkbox").val(); // the message id
var starImg = line.find(".star_clicker img"); // the star image

关于javascript - 如何在 jQuery 中删除选定的消息(带有复选框)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8002068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com