开发者

rails3-jquery-autocomplete clear field if no data available

I am using rails3-jquery-autocomplete. Is it possible to clear the value of an autocomplete field if the autocomplete returns no开发者_开发知识库 results? I'm trying to prevent users from submitting values we don't have in database.


I am using an older version but I implemented a change handler for the autocomplete() call.

$('input[data-autocomplete]').live('focus', function(i){
  $(this).autocomplete({
    // ...
    change: function(event, ui) {
      var $this = $(this);

      // blank if nothing was selected
      if (!ui.item) {
        $this.val(null);

        var id_selector = $this.attr('id_element');

        if (id_selector)
          $(id_selector).val(null);
      }
    }
  });
});


you can use select callback function to check if the value is correct, if its not then clear this input

$( ".selector" ).autocomplete({
   select: function(event, ui) { ... }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜