在下拉列表中显示与选项对应的数组中的数据

Display data from array corresponding to option in a dropdown

我正在尝试显示数组中的数据。我从该数组中获取国家/地区,但我需要在从下拉选项中选择后显示数据。

我需要在 .price-call p 中输入 'making_calls' 值,在 .price-text p 中输入 'sending_texts' 值。

这是代码

        array =[
          {
            "name": "Afghanistan",
            "dial_code": "+93",
            "code": "AF",
            "making_calls": "€0.30",
            "sending_texts": "€0.10",
            "call_setup": "€0.15"
        },
        {
            "name": "Albania",
            "dial_code": "+355",
            "code": "AL",
            "making_calls": "€0.6",
            "sending_texts": "€0.10",
            "call_setup": "€0.15"
        },
        {
            "name": "Algeria",
            "dial_code": "+213",
            "code": "DZ",
            "making_calls": "€1.20",
            "sending_texts": "€0.10",
            "call_setup": "€0.15"
        }
        ];
        
        $.each(array, function (i, p) {
          $('#selectCountry').append($('<option></option>').val(p.code).html(p.name + ' ' +  '(' + p.dial_code+  ')'  ));
        });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="" id="selectCountry">
   <option selected disabled>Country</option>
</select>

<div class="calls">
  <p>Making Calls</p>
  <p class="price-call">0.30$</p>
</div>
<div class="texts">
  <p>Sending Texts</p>
  <p class="price-text">0.15$</p>
</div>

如果下拉列表中的条件发生变化并比较您的国家/地区代码并找到所需的结果。

array = [{
    "name": "Afghanistan",
    "dial_code": "+93",
    "code": "AF",
    "making_calls": "€0.30",
    "sending_texts": "€0.10",
    "call_setup": "€0.15"
  },
  {
    "name": "Albania",
    "dial_code": "+355",
    "code": "AL",
    "making_calls": "€0.6",
    "sending_texts": "€0.10",
    "call_setup": "€0.15"
  },
  {
    "name": "Algeria",
    "dial_code": "+213",
    "code": "DZ",
    "making_calls": "€1.20",
    "sending_texts": "€0.10",
    "call_setup": "€0.15"
  }
];

$.each(array, function(i, p) {
  $('#selectCountry').append($('<option></option>').val(p.code).html(p.name + ' ' + '(' + p.dial_code + ')'));
});

$('#selectCountry').on('change', function() {
  var country = $(this).val();
  $.each(array, function(i, p) {
    if (p.code == country) {
      $('.price-call').html(p.making_calls);
      $('.price-text').html(p.sending_texts);
        return false; // break if match
    }

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="" id="selectCountry">
  <option selected disabled>Country</option>
</select>

<div class="calls">
  <p>Making Calls</p>
  <p class="price-call">0.30$</p>
</div>
<div class="texts">
  <p>Sending Texts</p>
  <p class="price-text">0.15$</p>
</div>

您可以使用 change 来获取 select 值,然后您可以使用 $.each 来检查数组中具有 if 条件的数据,然后附加 making_callssending_texts 到他们的元素。

var countries =[
      {
        "name": "Afghanistan",
        "dial_code": "+93",
        "code": "AF",
        "making_calls": "€0.30",
        "sending_texts": "€0.10",
        "call_setup": "€0.15"
    },
    {
        "name": "Albania",
        "dial_code": "+355",
        "code": "AL",
        "making_calls": "€0.6",
        "sending_texts": "€0.10",
        "call_setup": "€0.15"
    },
    {
        "name": "Algeria",
        "dial_code": "+213",
        "code": "DZ",
        "making_calls": "€1.20",
        "sending_texts": "€0.10",
        "call_setup": "€0.15"
    }
];
        
$.each(countries, function (i, p) {
    $('#selectCountry').append($('<option></option>').val(p.code).html(p.name + ' ' +  '(' + p.dial_code+  ')'  ));
});

$('#selectCountry').on('change',function(){
    var country = $(this).val();
    $.each(countries, function (key, val) {
        if( val.code == country ){
            $('.price-call').html(val.making_calls);
            $('.price-text').html(val.sending_texts);
            return false; // break loop if conditions match
        }
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="" id="selectCountry">
    <option selected disabled>Country</option>
</select>

<div class="country-info">
    <div class="calls">
        <p>Making Calls</p>
        <p class="price-call">0.30$</p>
    </div>
    <div class="texts">
        <p>Sending Texts</p>
        <p class="price-text">0.15$</p>
    </div>
</div>