Home > Web Front-end > JS Tutorial > 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
Release: 2016-05-16 15:28:08
Original
1589 people have browsed it

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:

Drop-down list:

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);
   }
  });
   }
 });
 );
});

Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template