将API返回的数据绑定到下拉列表

时间:2019-03-14 15:33:32

标签: javascript jquery json drop-down-menu

我正在尝试通过javascript / jquery绑定下拉列表(任何人都可以使用)。这是我尝试的代码-

$(document).ready(function () {  
   $.ajax({  
       type:"POST",
     url: "function.php",
     //dataType: "json"
     success: function(result){
     alert(result);
        for (var i in result) {
            $("#dropdownlist").append('<option value="'+result[i]+'">'+result[i]+'</option>');
    };

     }
  });  

});

处于警报状态时,它正在显示这样的数据-

  

{“ BU_CODE_RU”:“ DK”} {“ BU_CODE_RU”:“ PL”} {“ BU_CODE_RU”:“ SA”} {“ BU_CODE_RU”:“ SP”} {“ BU_CODE_RU”:“ RS”} {“ BU_CODE_RU“:” IS“} {” BU_CODE_RU“:” SE“} {” BU_CODE_RU“:” LT“} {” BU_CODE_RU“:” GR“} {” BU_CODE_RU“:” AT“} {” BU_CODE_RU“:” DE “} {” BU_CODE_RU“:” TR“} {” BU_CODE_RU“:” CZ“} {” BU_CODE_RU“:” US“} {” BU_CODE_RU“:” TW“} {” BU_CODE_RU“:” TH“} {” BU_CODE_RU “:” EG“} {” BU_CODE_RU“:” SI“} {” BU_CODE_RU“:” HU“} {” BU_CODE_RU“:” JP“} {” BU_CODE_RU“:” IN“} {” BU_CODE_RU“:” CA“ } {“ BU_CODE_RU”:“ UNK”} {“ BU_CODE_RU”:“ MA”} {“ BU_CODE_RU”:“ NL”} {“ BU_CODE_RU”:“ RU”} {“ BU_CODE_RU”:“ HK”} {“ BU_CODE_RU” :“ ID”} {“ BU_CODE_RU”:“ FR”} {“ BU_CODE_RU”:“ IT”} {“ BU_CODE_RU”:“ ES”} {“ BU_CODE_RU”:“ NO”} {“ BU_CODE_RU”:“ BE”} {“ BU_CODE_RU”:“ KR”} {“ BU_CODE_RU”:“ MY”} {“ BU_CODE_RU”:“ FI”} {“ BU_CODE_RU”:“ DO”} {“ BU_CODE_RU”:“ KW”} {“ BU_CODE_RU”: “ SK”} {“ BU_CODE_RU”:“ GB”} {“ BU_CODE_RU”:“ AE”} {“ BU_CODE_RU”:“ IL”} {“ BU_CODE_RU”:“ BG”} {“ BU_CODE_RU”:“ AUW”} { “ BU_CODE_RU”:“ SG”} {“ BU_CODE_RU”:“ BH”} {“ BU_CODE_RU”:“ QA”} {“ BU_CODE_RU”:“ LV”} {“ BU_CODE_RU”:“ AU”} {“ BU_CODE_RU”:“ HR“} {” BU_CODE_RU“:” CY“} {” BU_CODE_RU“:” IE“} {” BU_CO DE_RU“:” UA“} {” BU_CODE_RU“:” CE“} {” BU_CODE_RU“:” CN“} {” BU_CODE_RU“:” CH“} {” BU_CODE_RU“:” RO“} {” BU_CODE_RU“:” PT “} {” BU_CODE_RU“:” PH“} {” BU_CODE_RU“:” JO“}

如何将这些值绑定到下拉列表(仅值)。如果可以添加“选择国家/地区”之类的默认值会更好。

1 个答案:

答案 0 :(得分:2)

如果响应是字符串,则可以使用正则表达式

const data = `{"BU_CODE_RU":"DK"}{"BU_CODE_RU":"PL"}{"BU_CODE_RU":"SA"}{"BU_CODE_RU":"SP"}{"BU_CODE_RU":"RS"}{"BU_CODE_RU":"IS"}{"BU_CODE_RU":"SE"}{"BU_CODE_RU":"LT"}{"BU_CODE_RU":"GR"}{"BU_CODE_RU":"AT"}{"BU_CODE_RU":"DE"}{"BU_CODE_RU":"TR"}{"BU_CODE_RU":"CZ"}{"BU_CODE_RU":"US"}{"BU_CODE_RU":"TW"}{"BU_CODE_RU":"TH"}{"BU_CODE_RU":"EG"}{"BU_CODE_RU":"SI"}{"BU_CODE_RU":"HU"}{"BU_CODE_RU":"JP"}{"BU_CODE_RU":"IN"}{"BU_CODE_RU":"CA"}{"BU_CODE_RU":"UNK"}{"BU_CODE_RU":"MA"}{"BU_CODE_RU":"NL"}{"BU_CODE_RU":"RU"}{"BU_CODE_RU":"HK"}{"BU_CODE_RU":"ID"}{"BU_CODE_RU":"FR"}{"BU_CODE_RU":"IT"}{"BU_CODE_RU":"ES"}{"BU_CODE_RU":"NO"}{"BU_CODE_RU":"BE"}{"BU_CODE_RU":"KR"}{"BU_CODE_RU":"MY"}{"BU_CODE_RU":"FI"}{"BU_CODE_RU":"DO"}{"BU_CODE_RU":"KW"}{"BU_CODE_RU":"SK"}{"BU_CODE_RU":"GB"}{"BU_CODE_RU":"AE"}{"BU_CODE_RU":"IL"}{"BU_CODE_RU":"BG"}{"BU_CODE_RU":"AUW"}{"BU_CODE_RU":"SG"}{"BU_CODE_RU":"BH"}{"BU_CODE_RU":"QA"}{"BU_CODE_RU":"LV"}{"BU_CODE_RU":"AU"}{"BU_CODE_RU":"HR"}{"BU_CODE_RU":"CY"}{"BU_CODE_RU":"IE"}{"BU_CODE_RU":"UA"}{"BU_CODE_RU":"CE"}{"BU_CODE_RU":"CN"}{"BU_CODE_RU":"CH"}{"BU_CODE_RU":"RO"}{"BU_CODE_RU":"PT"}{"BU_CODE_RU":"PH"}{"BU_CODE_RU":"JO"}`

// success: function(data) {
 
   var options = data.match(/:"(\w+)/g)
  .map(cc => { 
    cc = cc.replace(/\W+/g,""); // remove non-letters
    return `<option value="${cc}">${cc}</option>`;
  });
 
   $("#dropdownlist").append(options);
 
// }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="dropdownlist">
<option value="">Please select</option>
</select>  

林恩(Jonathan Lam)向我指出,即使使用后向观察,也更为优雅

const data = `{"BU_CODE_RU":"DK"}{"BU_CODE_RU":"PL"}{"BU_CODE_RU":"SA"}{"BU_CODE_RU":"SP"}{"BU_CODE_RU":"RS"}{"BU_CODE_RU":"IS"}{"BU_CODE_RU":"SE"}{"BU_CODE_RU":"LT"}{"BU_CODE_RU":"GR"}{"BU_CODE_RU":"AT"}{"BU_CODE_RU":"DE"}{"BU_CODE_RU":"TR"}{"BU_CODE_RU":"CZ"}{"BU_CODE_RU":"US"}{"BU_CODE_RU":"TW"}{"BU_CODE_RU":"TH"}{"BU_CODE_RU":"EG"}{"BU_CODE_RU":"SI"}{"BU_CODE_RU":"HU"}{"BU_CODE_RU":"JP"}{"BU_CODE_RU":"IN"}{"BU_CODE_RU":"CA"}{"BU_CODE_RU":"UNK"}{"BU_CODE_RU":"MA"}{"BU_CODE_RU":"NL"}{"BU_CODE_RU":"RU"}{"BU_CODE_RU":"HK"}{"BU_CODE_RU":"ID"}{"BU_CODE_RU":"FR"}{"BU_CODE_RU":"IT"}{"BU_CODE_RU":"ES"}{"BU_CODE_RU":"NO"}{"BU_CODE_RU":"BE"}{"BU_CODE_RU":"KR"}{"BU_CODE_RU":"MY"}{"BU_CODE_RU":"FI"}{"BU_CODE_RU":"DO"}{"BU_CODE_RU":"KW"}{"BU_CODE_RU":"SK"}{"BU_CODE_RU":"GB"}{"BU_CODE_RU":"AE"}{"BU_CODE_RU":"IL"}{"BU_CODE_RU":"BG"}{"BU_CODE_RU":"AUW"}{"BU_CODE_RU":"SG"}{"BU_CODE_RU":"BH"}{"BU_CODE_RU":"QA"}{"BU_CODE_RU":"LV"}{"BU_CODE_RU":"AU"}{"BU_CODE_RU":"HR"}{"BU_CODE_RU":"CY"}{"BU_CODE_RU":"IE"}{"BU_CODE_RU":"UA"}{"BU_CODE_RU":"CE"}{"BU_CODE_RU":"CN"}{"BU_CODE_RU":"CH"}{"BU_CODE_RU":"RO"}{"BU_CODE_RU":"PT"}{"BU_CODE_RU":"PH"}{"BU_CODE_RU":"JO"}`

// success: function(data) {
  $("#dropdownlist").append(
    data.match(/(?<=:")(\w+)/g)
    .map(cc => `<option value="${cc}">${cc}</option>`)
  )

// }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="dropdownlist">
  <option value="">Please select</option>
</select>

注意:浏览器对JS RegExp后向(ES2018草案)的支持仍然很低。有关更多详细信息,请参见compatibility table on MDN


使用简单格式({"DK"}{"PL"}{"SA"}....)的原始答案:

var options = data.match(/[A-Z]{2,}/g).map(cc => `<option value="${cc}">${cc}</option>`)

const data = `{"DK"}{"PL"}{"SA"}{"SP"}{"RS"}{"IS"}{"SE"}{"LT"}{"GR"}{"AT"}{"DE"}{"TR"}{"CZ"}{"US"}{"TW"}{"TH"}{"EG"}{"SI"}{"HU"}{"JP"}{"IN"}{"CA"}{"UNK"}{"MA"}{"NL"}{"RU"}{"HK"}{"ID"}{"FR"}{"IT"}{"ES"}{"NO"}{"BE"}{"KR"}{"MY"}{"FI"}{"DO"}{"KW"}{"SK"}{"GB"}{"AE"}{"IL"}{"BG"}{"AUW"}{"SG"}{"BH"}{"QA"}{"LV"}{"AU"}{"HR"}{"CY"}{"IE"}{"UA"}{"CE"}{"CN"}{"CH"}{"RO"}{"PT"}{"PH"}{"JO"}`

// success: function(data) {
var options = data.match(/[A-Z]{2,}/g).map(cc => `<option value="${cc}">${cc}</option>`)
$("#dropdownlist").append(options);
// }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="dropdownlist">
  <option value="">Please select</option>
</select>