我能够使用jQuery Ajax从JSON文件添加/删除整个对象。但是我不知道如何从对象中删除或添加特定值。
这是我的JSON文件中的对象“代理”的第一项,另外还有7个ID为2-8的对象,
"agents": [{
"name": "bjstdmngbdr01",
"os": "windows",
"status": "idle",
"type": "physical",
"ip": "192.168.1.102",
"location": "/var/lib/test",
"resources": [
"Firefox",
"Safari",
"Ubuntu",
"Chrome"
],
"id": 1
}]
我真的很想知道如何删除“资源”中的值,例如,如果我单击按钮A,则删除“ Firefox”;如果我单击按钮B,则删除“ Safari”。
附加HTML部分:
<div class='sitecontent--agents__item'>
<div class='item-image'> <img src='images/" + agent.os + ".png' alt=''> </div>
<div class='item-right'>
<div class='item-name'>
<p> <i class='icon-desktop'></i>" + agent.name + "<span class='item-status "+ agent.status +"'>" + agent.status + "</span> </p>
</div>
<div class='item-ip-path'>
<div class='item-ip'>
<p><i class='icon-info'></i>" + agent.ip + "</p>
</div>
<div class='item-path'>
<p><i class='icon-info'></i>" + agent.location + "</p>
</div>
</div>
<div class='item-resources'>
<div class='resources-add'> <i class='icon-plus'></i> </div>
<div class='existingResources'>
<div class='existingResources__item'> <span> " + agent.resources[0] + " <button data-id='1' class='icon-trash remove'></button> </span> </div>
<div class='existingResources__item'> <span> " + agent.resources[1] + " <button data-id='2' class='icon-trash remove''></button> </span> </div>
<div class='existingResources__item'> <span> " + agent.resources[2] + " <button data-id='3' class='icon-trash remove''></button> </span> </div>
<div class='existingResources__item'> <span> " + agent.resources[3] + " <button data-id='4' class='icon-trash remove''></button> </span> </div>
</div>
</div>
<div class='test'>"+ agent.resources + " </div>
</div>
</div>
jQuery ajax删除:
$('.sitecontent--agents__listing').delegate('.remove','click',function(){<br>
var $item = $(this).closest('.existingResources__item');
$.ajax({
url: '/agents/' + $(this).attr('data-id'),
type: 'delete',
data: {},
success: function(){
$item.fadeOut(500,function(){
$(this).remove();
});
}
});
});
在我的html中,每个资源都有自己的div和一个删除按钮。单击按钮时,应从前端和json文件中删除该特定资源。现在,我不知道要这样做。