JavaScript在自动完成选择后清空输入

时间:2016-09-14 12:23:49

标签: javascript jquery autocomplete

我有这个代码工作正常,但我想清空输入$项目 用户选择自动填充建议后..

$number

在触发自动填充后,为了执行操作而放置此代码的位置?

   $project.autocomplete({
      minLength: 1,
      source: projects,
      focus: function( event, ui ) {
         $project.val(ui.item.label);
         return false;
      },
      select: function (e, ui) {
         createTag(ui.item.label, ui.item.id);
    }
  });

2 个答案:

答案 0 :(得分:1)

您需要使用close event



var projects = [
  "ActionScript",
  "AppleScript",
  "Asp",
  "BASIC",
  "C",
  "C++",
  "Clojure",
  "COBOL",
  "ColdFusion",
  "Erlang",
  "Fortran",
  "Groovy",
  "Haskell",
  "Java",
  "JavaScript",
  "Lisp",
  "Perl",
  "PHP",
  "Python",
  "Ruby",
  "Scala",
  "Scheme"
];

var $project = $( "#tags" );
$project.autocomplete({
  minLength: 1,
  source: projects,
  focus: function( event, ui ) {
    $project.val(ui.item.label);
    return false;
  },
  close: function (e, ui) {
    e.target.value = '';
    //createTag(ui.item.label, ui.item.id);
  }
});

<link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>

<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags">
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个解决方案

dialog.matches('help',[
    function (session, args, next) {builder.Prompts.choice(session, "I can help you to shop products for your lifestyle. You can buy from the following categories.", "Computer|TV|See more", { listStyle: builder.ListStyle["list"] });

    },
    function (session, results) {
        if (results.response.entity=='TV') {
            session.send("Wow. That sounds interesting one. We have many %s models. Ask me question like \"I want to buy black color Samsung LED Tv .\"", results.response.entity);
            session.endDialog();
        }else{
            session.send("Selected category is expected soon.");
            session.endDialog();
        }           
    }]);