点击按钮

时间:2016-06-21 11:02:53

标签: javascript jquery autocomplete

我有自动完成的波纹管代码。我需要通过单击按钮来更改自动完成的值。但是波纹管代码不起作用。

$(function() {
  var countries_starting_with_A = [{
    "id": "1",
    "value": "Afghanistan",
    "label": "Afghanistan"
  }, {
    "id": "17",
    "value": "Albania",
    "label": "Albania"
  }, {
    "id": "18",
    "value": "Algeria",
    "label": "Algeria"
  }, {
    "id": "20",
    "value": "American Samoa",
    "label": "American Samoa"
  }, {
    "id": "22",
    "value": "Andorra",
    "label": "Andorra"
  }, {
    "id": "10",
    "value": "Angola",
    "label": "Angola"
  }, {
    "id": "11",
    "value": "Anguilla",
    "label": "Anguilla"
  }, {
    "id": "23",
    "value": "Antarctica",
    "label": "Antarctica"
  }, {
    "id": "24",
    "value": "Antigua and Barbuda",
    "label": "Antigua and Barbuda"
  }, {
    "id": "25",
    "value": "Argentina",
    "label": "Argentina"
  }, {
    "id": "26",
    "value": "Armenia",
    "label": "Armenia"
  }, {
    "id": "27",
    "value": "Aruba",
    "label": "Aruba"
  }, {
    "id": "28",
    "value": "Australia",
    "label": "Australia"
  }, {
    "id": "29",
    "value": "Austria",
    "label": "Austria"
  }, {
    "id": "12",
    "value": "Azerbaijan",
    "label": "Azerbaijan"
  }];

  $("#field").autocomplete({
    source: countries_starting_with_A,
    minLength: 1,
    select: function(event, ui) {
    },
    // mustMatch implementation
    change: function(event, ui) {
      if (ui.item === null) {
        $(this).val('');
      }
    }
  });
  $('#setvalue').click(function() {
    $('#field').data('ui-autocomplete')._trigger('select', 'autocompleteselect', {
      item: {
        "id": "29",
        "label": "Austria",
        "value": "Austria"
      }
    });
  });

});
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<input id="field" type="text" style="width: 200px;">
<button id="setvalue">SET VALUE</button>

0 个答案:

没有答案