jQuery Autocomplete source dependent from different input

元气小坏坏 提交于 2021-01-28 00:18:34

问题


The solution for only 2 options is posted here @VinayC :
jQuery UI - Autocomplete source dependent from selected option in different input

But I need a solution for more than two options in my select?
The code is here: http://jsbin.com/fakuwohupa/edit?html,js,output

<select id="country">
    <option value="">Choice one</option>
    <option value="1">US</option>
    <option value="2">UK</option>
    <option value="3">GR</option>
    <option value="4">IT</option>
</select>
<form id="aspnetForm" action="" method="post">
    Type "A" to test:<input type="text" id="city" >
</form>
$(function() {
    var US = [ "City1", "City2", "City3" ];
    var UK = [ "UK_City1", "UK_City2", "UK_City3" ];
    var GR = [ "Gr_City1", "Gr_City2", "Gr_City3" ];
    var IT = [ "It_City1", "It_City2", "It_City3" ];

    $('#country').change(function() {
        var src = $('#country').val() == '1' ? US : UK;
        $("#city").autocomplete('option', 'source', src);
    });

    $("#city").autocomplete({
        source: []
    });
});

Thank you


回答1:


$(function() {
    var sources = {
        US : [ "City1", "City2", "City3" ],
        UK : [ "UK_City1", "UK_City2", "UK_City3" ],
        GR : [ "Gr_City1", "Gr_City2", "Gr_City3" ],
        IT : [ "It_City1", "It_City2", "It_City3" ]
    }
    
    var $city = $("#city").autocomplete({
            source: []
        });
  
    $('#country').change(function() {
            $("#city").val('');
            var src = $(this).find("option:selected").text();
            $city.autocomplete('option', 'source', sources[src]);
        });
        
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
   
 <select id="country">
  <option value="">Choice one</option>
      <option value="1">US</option>
      <option value="2">UK</option>
      <option value="3">GR</option>
      <option value="4">IT</option>
 </select>
 <form id="aspnetForm" action="" method="post">
      Search City:
      <input type="text" id="city" >
 </form>



回答2:


This is the code I am using for passing more then one value. Using this you can pass as many value you want in autocomplete

$("#ServiceName").autocomplete({
            minLength: 3,
            source: '@Url.Action("FillSelectedSeriesName")',
            data: { series_id: $("#SeriesName option:selected").text(), series_name: document.getElementById("SeriesName").value },
            source: function (request, response) {
                var term = request.term;
                var series_id = document.getElementById("SeriesName").value;
                var series_name = $("#SeriesName option:selected").text();
                $.getJSON("FillSelectedSeriesName?term=" + term + '&series_id=' + series_id + '&series_name=' + series_name, request, function (data, status, xhr) {
                    response(data);
                });

            },
            select: function (event, ui) {
                $('#ServiceName').val(ui.item.value);
                $('#ServiceID').val(ui.item.id);
            },
            change: function (event, ui) {
            }
        });


来源:https://stackoverflow.com/questions/31300396/jquery-autocomplete-source-dependent-from-different-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!