检查自动填充是否为空

时间:2015-12-22 22:05:49

标签: jquery autocomplete

我正在使用jquery ui。我目前正在使用自动完成小部件。基本上我有一个按钮,想知道用户是否在该自动完成框中输入了任何内容。如果没有,那么使一个字符串等于null,否则得到测试并将其分配给var。

这是代码:

$(function() {
var availableTags = [
  "The Alps",
  "Europe",
  "Africa",
  "The Caribbean",
  "Indian Ocean",
  "North and Central America",
  "South America",
  "Asia"
];
$( "#destination" ).autocomplete({
  source: availableTags
});
});

$('#destination').on('autocompleteselect', function (e, ui) {
 var ii = ui.item.value;
 alert(ii);
    if(ii == "" || ii == null || ii == undefined){

    }else{
     destination(ii);
    }
  });

function destination(nameOfResort){
        destination = nameOfResort;
        alert("name " + destination);
    }

正如您在if语句中看到的那样,如果自动完成框为空,那么除了调用警报时它不会执行任何操作,它会在弹出框中显示以下内容:

name  function destination(nameOfResort){
            destination = nameOfResort;
        }

1 个答案:

答案 0 :(得分:0)

您尚未指定确切问题,但如果您的代码无效,请尝试将autocompleteselect放入$(function()

$(function() {
        var availableTags = [
          "The Alps",
          "Europe",
          "Africa",
          "The Caribbean",
          "Indian Ocean",
          "North and Central America",
          "South America",
          "Asia"
        ];
        $( "#destination" ).autocomplete({
          source: availableTags

        });
        $('#destination').on('autocompleteselect', function (e, ui) {
            var ii = ui.item.value;
            alert(ii);
            if(ii == "" || ii == null || ii == undefined){

            }else{
                destination(ii);
            }
         });
    });