How to fill form with JSON?

前端 未结 11 1586
灰色年华
灰色年华 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:37

    Assuming data is the JSON object, you could use this inside the $.getJSON callback:

    var $inputs = $('form input');
    $.each(data, function(key, value) {
      $inputs.filter(function() {
        return key == this.name;
      }).val(value);
    });
    

提交回复
热议问题