Home  >  Article  >  Web Front-end  >  jquery implements a method to update the contents of a drop-down list when triggered_jquery

jquery implements a method to update the contents of a drop-down list when triggered_jquery

WBOY
WBOYOriginal
2016-05-16 15:28:081570browse

The example in this article describes how jquery implements the method of updating the contents of a drop-down list when triggered. Share it with everyone for your reference, the details are as follows:

If the server has a request address to return the required data

url:/hello

Returns something like:

Copy code The code is as follows:
[{"Watermelon":10},{"Apple":12},{ "Banana":13},{"Mango":14}]

Such json data

html:

Button: 281ce987ab40a99840e384e87fddb38a

Drop-down list:28aacf2a2a285412e07f89f779e8c8c918bb6ffaf0152bbe49cd8a3620346341

js code:

$(function(){
  $("#btn").click(
   $.ajax({
  type:"POST",
  url:"http://localhost/XXXX/Test",
  cache: false,//不缓存
  dataType:"json",//返回数据格式
  success:function(ret){ 
    $("#sel").empty(); 
    $.each(ret,function(ind){
    for(var key in ret[ind]){
      var opt = $("<option></option>");
     opt.val(ret[ind][key]);
    opt.text(key);
    $("#sel").append(opt);
   }
  });
   }
 });
 );
});

I hope this article will be helpful to everyone in jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn