How to fill form with JSON?

前端 未结 11 1581
灰色年华
灰色年华 2020-12-29 14:17

I get ajax response as JSON and need to fill a form with it. How to do that in jQuery or something else ? Is something better than using $(json).each() ?

<
11条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-29 14:49

    var json={ 
      "id" : 12,
      "name": "Jack",
      "description": "Description"
    };
    for(key in json)
    {
      if(json.hasOwnProperty(key))
        $('input[name='+key+']').val(json[key]);
    }
    

    srry i thought it was the id property that was set.

    here: http://jsfiddle.net/anilkamath87/XspdN/

提交回复
热议问题