“Walk” JSON response and populate form fields — more efficient approach?

前端 未结 4 718
南笙
南笙 2020-12-13 16:36

I know there is a far more elegant/efficient way of doing this (in php I would use foreach) but with jQuery how can I walk the var/val pairs of a JSON response and populate

4条回答
  •  旧巷少年郎
    2020-12-13 17:18

    You can get rid of all "if" statements by replacing your $.each with this:

    $.each(data, function(i, item){
      $("#"+item.field).val(item.value);
    });
    

提交回复
热议问题