第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 ajax 將 JSON 數(shù)組放入 Select Tag 中僅獲取 1 個(gè)值

使用 ajax 將 JSON 數(shù)組放入 Select Tag 中僅獲取 1 個(gè)值

12345678_0001 2023-10-17 16:57:23
我想獲取我調(diào)用到選擇選項(xiàng)標(biāo)簽的 json 數(shù)組的所有數(shù)據(jù)我從這里調(diào)用 api http://dev.farizdotid.com/api/daerahindonesia/provinsi這是我得到的 json 數(shù)組semuaprovinsi: Array(34)0: {id: "11", nama: "Aceh"}1: {id: "12", nama: "Sumatera Utara"}2: {id: "13", nama: "Sumatera Barat"}3: {id: "14", nama: "Riau"}4: {id: "15", nama: "Jambi"}5: {id: "16", nama: "Sumatera Selatan"}6: {id: "17", nama: "Bengkulu"}7: {id: "18", nama: "Lampung"}8: {id: "19", nama: "Kepulauan Bangka Belitung"}9: {id: "21", nama: "Kepulauan Riau"}10: {id: "31", nama: "Dki Jakarta"}11: {id: "32", nama: "Jawa Barat"}12: {id: "33", nama: "Jawa Tengah"}13: {id: "34", nama: "Di Yogyakarta"}14: {id: "35", nama: "Jawa Timur"}15: {id: "36", nama: "Banten"}16: {id: "51", nama: "Bali"}17: {id: "52", nama: "Nusa Tenggara Barat"}18: {id: "53", nama: "Nusa Tenggara Timur"}19: {id: "61", nama: "Kalimantan Barat"}20: {id: "62", nama: "Kalimantan Tengah"}21: {id: "63", nama: "Kalimantan Selatan"}22: {id: "64", nama: "Kalimantan Timur"}23: {id: "65", nama: "Kalimantan Utara"}24: {id: "71", nama: "Sulawesi Utara"}25: {id: "72", nama: "Sulawesi Tengah"}26: {id: "73", nama: "Sulawesi Selatan"}27: {id: "74", nama: "Sulawesi Tenggara"}28: {id: "75", nama: "Gorontalo"}29: {id: "76", nama: "Sulawesi Barat"}30: {id: "81", nama: "Maluku"}31: {id: "82", nama: "Maluku Utara"}32: {id: "91", nama: "Papua Barat"}33: {id: "94", nama: "Papua"}這是我的 jquery 代碼$(function() {    var province = $('#provinces');    var select = $('#selection');    var option = $('<option>');            });        }    });});它只顯示進(jìn)入我的選擇標(biāo)簽的數(shù)據(jù)的最后一個(gè)索引,即索引號(hào) 33這是我選擇的標(biāo)簽<select id="selection">   <option value=""></option></select>我不知道為什么我只得到 1 個(gè)數(shù)據(jù)
查看完整描述

2 回答

?
慕桂英546537

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊

您必須為每個(gè)選項(xiàng)創(chuàng)建新選項(xiàng)。否則修改相同。


$(function() {

  var province = $("#provinces");

  var select = $("#selection");


  $.ajax({

    type: "GET",

    dataType: "json",

    url: "http://dev.farizdotid.com/api/daerahindonesia/provinsi",

    success: function(provinces) {

      console.log(provinces);


      var allProvince = provinces.semuaprovinsi;


      $.each(allProvince, function(i, item) {

        var option = $("<option>");

        option.text(item.nama);

        option.attr("value", item.id);

        select.append(option);

      });

    }

  });

});

$(function() {

  var select = $("#selection");

  $.ajax({

    type: "GET",

    dataType: "json",

    url: "http://dev.farizdotid.com/api/daerahindonesia/provinsi",

    success: function(provinces) {

      var allProvince = provinces.semuaprovinsi;

      $.each(allProvince, function(i, item) {

        var option = $("<option>");

        option.text(item.nama);

        option.attr("value", item.id);

        select.append(option);

      });

    }

  });

});

**//Working sample**

<!DOCTYPE html>

<html>


<head>

   <script src="https://code.jquery.com/jquery-3.4.1.min.js"

      integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>

   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.3/js/jquery.tablesorter.min.js"></script>

</head>

<body>

   <select id="selection">

      <option value=""></option>

   </select>

   

</body>


</html>


查看完整回答
反對(duì) 回復(fù) 2023-10-17
?
慕雪6442864

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

在每次迭代option中初始化:.each


$.each(allProvince, function (i, item) {

    var option = $('<option>');

    option.text(item.nama); 

    option.attr('value', item.id);

    select.append(option);

});


查看完整回答
反對(duì) 回復(fù) 2023-10-17
  • 2 回答
  • 0 關(guān)注
  • 153 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)