JQuery 选择的插件 - 将用户键入的输入发送到 Ajax 调用

JQuery chosen plugin - Sending the user typed input to an Ajax call

我正在尝试使用 Chosen 插件为多个 select 填充下拉列表。

我添加了 fiddle

http://jsfiddle.net/san1234/ymnj12xk/

我的目的是根据通过 Ajax 调用发送用户键入的字母获得的 JSON 数据,在 "select" 标签中填充 "options" 标签。

为此我需要知道,如何进行 ajax 调用 onkeyup 即如果用户键入 "Am",我想将该输入发送到 Ajax 调用并获取JSON 响应,类似于 ["America","Amsterdam"]

我是新手,我想不出一种方法来提取用户在 'select' 框中键入的输入,以便在 Ajax 调用中实际将其作为请求发送。

我试过这样做,但是 JS 文件中的 'autocomplete' 方法不起作用 我该怎么做呢?请帮忙!

JS文件

$(".chosen-select").chosen();

$(".chosen-select-deselect").chosen({
  allow_single_deselect: true
});
$('.chosen-choices input').autocomplete({
  source: function(request, response) {
    $.ajax({

      url: "/someURL/" + request.term + "/",
      dataType: "json",
      beforeSend: function() {
        $('ul.chosen-results').empty();
      },
      success: function(data) {
        alert("Success!");
        response($.map(data, function(item) {

          $('ul.chosen-results').append('<li class="active-result">' + item.name + '</li>');
        }));
      }
    });
  }
});
<link href="http://harvesthq.github.io/chosen/chosen.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://harvesthq.github.io/chosen/chosen.jquery.js"></script>
<select class="chosen chosen-select" multiple="" data-placeholder="Choose a Country" style="width:200px">
    <option value="America">America</option>
     <option value="Amsterdam">Amsterdam</option>
      <option value="Australia">Australia</option>
    <option value="Dallas">Dallas</option>
    <option value="GHI">hij</option>
</select>

我不知道您是否在后端或任何其他程序语言中使用 PHP,但这是我使用 php 和 javascript 的解决方案:

首先你的 javaScript:

//initialization of chosen select
$(".chosen-select").chosen({
  search_contains: true // an option to search between words
});
$(".chosen-select-deselect").chosen({
  allow_single_deselect: true
});

//ajax function to search a new value to the dropdown list
function _ajaxSearch (param){
  return $.ajax({
    url: "request.php",
    type: "POST",
    dataType: "json",
    data: {param:param}
  })
}
//key event to call our ajax call
$(".chosen-choices input").on('keyup',function(){
  var  param = $('.chosen-choices input').val();// get the pressed key
  _ajaxSearch(param)
  .done(function(response){
    var exists; // variable that returns a true if the value already exists in our dropdown list
    $.each(response,function(index, el) { //loop to check if the value exists inside the list
      $('#mySelect option').each(function(){
        if (this.value == el.key) {
          exists = true;
        }
      });
      if (!exists) {// if the value does not exists, added it to the list
        $("#mySelect").append("<option value="+el.key+">"+el.value+"</option>");
        var ChosenInputValue = $('.chosen-choices input').val();//get the current value of the search
        $("#mySelect").trigger("chosen:updated");//update the list
        $('.chosen-choices input').val(ChosenInputValue);//since the update method reset the input fill the input with the value already typed
      }
    });
  })
})

您的 php 文件:

if (isset($_POST["param"])) {// check is the param is set
$param = $_POST["param"]; // get the value of the param
$array  = array('JKL' => 'jkl' , 'MNO'=>'mno', 'PQR'=>'pqr','STU'=>'stu','VWX'=>'vwx','YZ'=>'yz' );//array of values
$matches = array();//array of matches
    foreach($array as $key=>$value){//loop to check the values
        //check for match.
        if(strpos($value, $param) !== false){
            //add to matches array.
            $matches[]= array ("key"=> $key,"value"=>$value);
        }
    }
//send the response using json format
echo json_encode($matches);
}

希望对您有所帮助