以下是我的json
回应:
{"fname":"abc","lname":"xyz","email":"test@user.com","description":[{"city":"abcxyz","address":"AX","country":"US","date":"2020-02-01"},{"city":"abcxyz","address":"AX","country":"US","date":"2020-02 01"}], "city":"abcxyz","address":"DS","country":"US","Month":"12","Year":"2012"}
以下是我的代码:
success: function(data)
{
var userinfo=eval(data);
alert(userinfo['fname1']; //display correct result
alert(userinfo['description']['city']; //display undefined
if(!$("#fname1").val()) $("#fname1").val(userinfo['fname']);
if(!$("#lname1").val()) $("#lname1").val(userinfo['lname']);
}
在成功功能中,如果我试图提醒,alert(userinfo['fname1']
那么它会向我显示正确的结果,即显示名称,但如果我试图提醒,alert(userinfo['description']['city']
那么它会向我显示undefined
警报。
然后我尝试使用each
循环但仍然无法正常工作
$.each(userinfo['description'], function() {
alert(userinfo'description']['city']; //not working
$("#city option[value="+userinfo['description']['city']+"]").attr("selected", "selected");
});
以下是我的html:
<select name="country" id="country">
<?php
foreach ($this->description as $country) {
?>
<option value="<?php echo $country['country']; ?>" <?php echo ($this->params['country'] == $country['country']) ? "selected='selected'" : ""; ?>><?php echo $country['country']; ?></option>
<?php }
?>
</select>
谁能告诉我哪里出错了。谢谢。