duankao4489 2019-07-09 12:00
浏览 102
已采纳

如何在jquery select2中获取项目

I want to take the value (flight_no) that is in the processResults function, so when I select the airline name the code from the airlines name must be filled with (flight_no)

I have tried to get (flight_no) that is in the processResults function, but which I only get (airline_id)

$('#airlines').select2({
        allowClear: true,
        placeholder: 'Select Airlines',
        ajax: {
            dataType: 'json',
            type: 'POST',
            delay: 250,
            url: '{{route('search-airlines')}}',
            data:function(param){
                return{
                    airlines:param.term
                }
            },
            processResults: function(data, param){
                return {
                    results: data.map(function(item){
                        return { id: item.airlines_id,text: item.airlines_name, slug: item.flight_no} 
                    })
                }
            }
        }
    });

    $('#codeAirline').click(function () {
        $('#output').text($('#airlines').val());
    });


<div class="col-12 clearfix">
    <label for=""><p class="mb-0">Airlines</p></label>
    <select id="airlines" name="airlines" style="width: 100%;max-height: 100%; background-color: #e8e4e473;" required>
            <option value="">Select airlines</option>
    </select>
</div>
<div class="col-4">
    <div class="form-group">
        <select class="form-control" id="codeAirline" style="background-color:#dcdcdc;">
            <option id="output"></option>
        </select>
    </div>
</div>

My json

{
    "DATA": [
        {
            "airlines_id": 1,
            "airlines_name": "Garuda Airlines",
            "status": "1",
            "flight_no": "GA"
        },
        {
            "airlines_id": 3,
            "airlines_name": "Sriwijaya Airlines",
            "status": "1",
            "flight_no": "SJ,SA"
        },
        {
            "airlines_id": 4,
            "airlines_name": "Susi Airlines",
            "status": "1",
            "flight_no": "SI"
        },
        {
            "airlines_id": 5,
            "airlines_name": "Multi Airlines",
            "status": "1",
            "flight_no": "MA,MU,MAS"
        },
   ]
}
  • 写回答

2条回答 默认 最新

  • duanpie2414 2019-07-09 12:49
    关注

    Add change event to your select element with id airlines. When the value is selected, get the selected airlines_id and find within the array, then assign the found flight_no to the select element with id codeAirline.

    const input = {
      "DATA": [{
          "airlines_id": 1,
          "airlines_name": "Garuda Airlines",
          "status": "1",
          "flight_no": "GA"
        },
        {
          "airlines_id": 3,
          "airlines_name": "Sriwijaya Airlines",
          "status": "1",
          "flight_no": "SJ,SA"
        },
        {
          "airlines_id": 4,
          "airlines_name": "Susi Airlines",
          "status": "1",
          "flight_no": "SI"
        },
        {
          "airlines_id": 5,
          "airlines_name": "Multi Airlines",
          "status": "1",
          "flight_no": "MA,MU,MAS"
        },
      ]
    };
    
    
    const airlinesDropdown = $('#airlines');
    const flightNum = $('#codeAirline');
    
    input["DATA"].forEach(({
      airlines_id,
      airlines_name,
      flight_no
    }) => {
      airlinesDropdown.append(`<option value="${airlines_id}">${airlines_name}</option>`);
      flightNum.append(`<option value="${flight_no}">${flight_no}</option>`);
    });
    
    $('#airlines').on('change', function() {
      const selectedAirline = $(this).val();
      const {
        flight_no
      } = input["DATA"].find(({
        airlines_id
      }) => airlines_id == +selectedAirline);
      console.log(flight_no);
      flightNum.val(flight_no);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="col-4">
      <select id="airlines" name="airlines" style="width: 100%;max-height: 100%; background-color: #e8e4e473;" required>
        <option value="">Select airlines</option>
      </select>
    </div>
    <br>
    <div class="col-4">
      <select class="form-control" id="codeAirline" style="background-color:#dcdcdc;">
        <option value="">Select</option>
      </select>
    </div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?