如何从输入值中删除特定的json对象?
我已经尝试过这个jquery javascript remove object data from JSON object并使用了splice。
它没有从输入值中删除对象。
html:
<input type="text" name="jsonKey" value='{"keyId":"1","price":"13.28"},{"keyId":"2","price":"15.00"}' style="width:100%">
<a href="#" class="delete">Delete</a>JS:
$('.delete').click(function(){
var json = $.parseJSON('[' + $('input[name=jsonKey]').val() + ']');
$.each(json, function(index, result) {
if(result['keyId'] == 1) {
//delete json[index];
//json.splice(index,1);
}
});
return false;
});您可以查看代码http://jsfiddle.net/Pwa92/
发布于 2013-01-24 20:40:54
根据Imrul对this question的回答,您可以使用$.grep
$('.delete').click(function(){
var json = $.parseJSON('[' + $('input[name=jsonKey]').val() + ']');
console.log(json);
json = jQuery.grep(
json,
function (item,index) {
return item.keyId != "1";
}
);
console.log(json);
return false;
});我更新了你的fiddle,在浏览器的js控制台打开的时候使用。
https://stackoverflow.com/questions/14501053
复制相似问题