大家好,我正在连接这样的JSON
字符串:
var addresses = "[";
addresses += '{"AddressID":' + adressid + ',"EmailID":' + $('#txtemailData').val() + ',"Hno":' + $('#txthno').val() + ',"StreetName":' + $('#txtstreetname').val() + ',"City":' + $('#txtcity').val() + ',"StateID":' + $('#ddlState').val() + ',"CountryID":' + $('#ddlcountry').val() + ',"Zip":' + $('#txtzip').val() + ',"PhoneNumber":' + $('#txtphonenumber').val() + ',"Fax":' + $('#txtfax').val() + ',"AddressName:' + $('#txtaddresstype').val() + '"},';
对象看起来像这样:
[{
"AddressID":2,
"EmailID":akanilkumar443@gmail.com,
"Hno":Hyderabad,
"StreetName":Gachibowli,
"City":Hyderabad,
"StateID":1,
"CountryID":1,
"Zip":040,
"PhoneNumber":8341516166,
"Fax":23123131,
"AddressName:Store Address"},
{
"AddressID":3,
"EmailID":akanilkumar443@gmail.com,
"Hno":aSAs,
"StreetName":asdasdad,
"City":asdasda,
"StateID":1,
"CountryID":1,
"Zip":asdasda,
"PhoneNumber":asdasda,
"Fax":asdasda,
"AddressName:Store Type"
}]
如何根据它的 id 更新 json 对象的这个特定值?
假设我想更改我的对象 where 的一些值AddressID=2
。例如,我想更改EmailID
,Streetname
的JSON
对象 where AddressID=2
。如何使用 jQuery 做到这一点?
我正在尝试这样,但它没有进入循环,任何人都可以在这里帮助我吗?
function EditAddress(addressid) {
alert(addressid);
alert(addresses);
var addressobject =JSON.parse(addresses.substring(0, addresses.length - 1) + ']');
jQuery.each(addressobject, function (i, val) {
alert(val.AddressID);
if (val.AddressID == addressid)
{
//update logic
}
});
}