简体   繁体   English

使用JSON回复,使用jQuery从列表中删除

[英]erase from list using JSON reply, using jQuery

I have this list of items, and I have a button at the bottom for deleting some of them according to their checkboxes. 我有此项目列表,并且在底部有一个按钮,用于根据其复选框删除其中的一些项目。 I also got a response from a jQuery get() with the elements that represent the new list. 我还从jQuery get()得到了一个响应,其中包含代表新列表的元素。 So, the rest need to be removed. 因此,其余部分需要删除。 This is how my list is built : 这是我的列表的构建方式:

<table style="float:left;width:100%">
<tbody>
    <?php foreach ($items as $item): ?>
    <tr>
        <div>
            <td ?>;
                <input type="checkbox" name="item_<?php echo $item['NAME'];?>" value="<?php echo $item['ID'];?>" />
            </td>
        </div>
    </tr>
    <?php endforeach; ?>
</tbody>

I use the get() function and get the following reply in json. 我使用get()函数并在json中得到以下答复。

[{"ID":"2","EMAIL":"xxxxx@hotmail.com","DATETIME":"2011-12-12 03:20:01"},{"ID":"4","EMAIL":"xxxxx@gmail.com","DATETIME":"2011-10-09 01:15:22"}"]

All I want to do now is simply eliminate the correspoding where the input value (which is the json's ID) is NOT in the json reply. 我现在要做的就是简单地消除相应的错误,即在json回复中没有输入值(这是json的ID)。

Any ideas how to do this with jQuery ? 有什么想法如何使用jQuery吗? Thanks ! 谢谢 !

There are several different ways to do this. 有几种不同的方法可以做到这一点。 I kind of like going through your json response, flagging those that are confirmed, delete those unconfirmed, and remove the confirmed flag. 我有点喜欢遍历您的json响应,标记已确认的那些,删除未确认的那些,并删除已确认的标志。 eg 例如

var json = [{'id':'2','email':... }];

// first, flag what we DO have
$.each(json,function(n,r){
  var $i = $(':input[value="' + r.ID + '"]');
  if ($i.length > 0){
    $i.addClass('ajax-confirmed');
  }
});
// delete what hasn't been flagged
$(':input:not(.ajax-confirmed)').each(function(m,r){
  var $row = $(r).closest('tr');
  $row.remove();
});
// then remove the flag (cleanup)
$(':input.ajax-confirmed').removeClass('ajax-confirmed');

Fiddle showing it in action: http://jsfiddle.net/v9paf/ (Or if you want to feel like you're deleting it... http://jsfiddle.net/v9paf/1/ ) 小提琴在操作中显示它: http : //jsfiddle.net/v9paf/ (或者,如果您想删除它.​​.. http://jsfiddle.net/v9paf/1/

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

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