简体   繁体   English

通过jquery删除多个用户复选框

[英]delete multiple users by jquery checkbox

I listed my friends info(usernames with avatars) in phpbb3 custom page and I need to delete multiple users by checking checkboxes. 我在phpbb3自定义页面中列出了我的朋友信息(带头像的用户名),我需要通过选中复选框来删除多个用户。

Here is my source code, but I don't know why it is not working. 这是我的源代码,但我不知道为什么它不起作用。

<form method="post" action="delete.php" class="form-inline">
<!-- BEGIN friends -->
<ul class="friends">
    <li id="{friends.F_ID}.li">
        <div class="media-group">
                <a class="pull-left" title="{friends.F_USERNAME}">
                    <img class="media-object img-circle img-avatar" src="{friends.F_AVATAR}">
                </a>

            <div style="width:30px; height:30px;">
            <input type="checkbox" name="friends[]" id="{friends.F_ID}">
            </div>

            <a href="#"><h4 class="media-heading">{friends.F_USERNAME}</h4></a>
            <span class="span-block">123..</span>
            <span class="span-block">{LAST_VISIT_DATE}</span>
        </div>
    </li>
</ul>
<!-- END friends -->
<button id="delete_friends" name="delete_friends" type="submit">Delete Friends</button>
</form>

And my javascript is this: 而我的JavaScript是这样的:

<script>
$("#delete_friends").click(function(){

var data = {'friends[]' : []};
$(":checked").each(function() {
 data['friends[]'].push($(this).val());
});

if (confirm("Are you sure you want to delete "+data+"?"))
{
    $("#loading_full").fadeIn('fast');
    $.post("delete.php", { user_id:'<?php echo $user_id; ?>' , friend_username : data })
    .done(function(data) {
    alert(data);
        if (data.toLowerCase().indexOf("success delete") >= 0){
            $("#"+data+"li").fadeOut('fast');
            $("#loading_full").fadeOut('fast');
        }
    })
    .fail(function(){
     });
     return false;
}
});
</script>

the way you are initializing the arrays i think is not good, you can do it like: 我认为不好的初始化数组的方式可以做到:

var data = {'friends' : []};

this is beacause anyway you are set 'friends' as an array; 这是因为无论如何将“朋友”设置为数组;

then you are pushing data['friends[]'].push("some value"); 然后您要推送data['friends[]'].push("some value");

i think is better if you something like: 我认为最好的方法是:

var friends = new Array();
$(":checked").each(function() {
 friends.push($(this).val());
});

var data = {'friends' : friends};

I tried to clear the concept how to get values from multiple selected check box below. 我试图弄清楚如何从下面的多个选中的复选框中获取值的概念。 I hope that will help you. 希望对您有所帮助。

In HTML use 在HTML中使用

<input type="checkbox" name="friends[]" value="{friends.F_ID}" id="{friends.F_ID}">

in the place of <input type="checkbox" name="friends[]" id="{friends.F_ID}"> 代替<input type="checkbox" name="friends[]" id="{friends.F_ID}">

HTML: HTML:

<form method="post" action="delete.php" class="form-inline">
<!-- BEGIN friends -->
<ul class="friends">
<li id="{friends.F_ID}.li">
    <div class="media-group">
            <a class="pull-left" title="{friends.F_USERNAME}">
             <img class="media-object img-circle img-avatar" src="{friends.F_AVATAR}">
            </a>

        <div style="width:30px; height:30px;">
        <input type="checkbox" name="friends[]" value="{friends.F_ID}" id="{friends.F_ID}">
        </div>

        <a href="#"><h4 class="media-heading">{friends.F_USERNAME}</h4></a>
        <span class="span-block">123..</span>
        <span class="span-block">{LAST_VISIT_DATE}</span>
    </div>
</li>
</ul>
<!-- END friends -->
<button id="delete_friends" name="delete_friends" type="submit">Delete Friends</button>

PHP Submit Code : PHP提交代码:

if(isset($_POST["submit"]) && $_POST["submit"]!="")
{
    $usersCount = count($_POST["friends"]);
    for($i=0;$i<$usersCount;$i++)
    {
       //mysql_query  // friends.F_ID = $_POST["friends"][$i]
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM