Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何展示';“没有结果”;在jquery自动完成中,是否正确?_Javascript_Jquery - Fatal编程技术网

Javascript 如何展示';“没有结果”;在jquery自动完成中,是否正确?

Javascript 如何展示';“没有结果”;在jquery自动完成中,是否正确?,javascript,jquery,Javascript,Jquery,我想在用户搜索不在列表中的查询时实现一条消息,以显示未找到搜索。我试着用 $('#searchTextField').html(" NO SEARCH FOUND"); 但它不起作用。有人知道如何用我下面的代码解决这个问题吗?谢谢你的帮助 这是我的密码: $(function () { var myData = []; myData.push("NO SEARCH FOUND"); $.get("http://localhost:8080/myApp/JobSearchItem.

我想在用户搜索不在列表中的查询时实现一条消息,以显示未找到搜索。我试着用

$('#searchTextField').html(" NO SEARCH FOUND");
但它不起作用。有人知道如何用我下面的代码解决这个问题吗?谢谢你的帮助

这是我的密码:

$(function () {
  var myData = [];
   myData.push("NO SEARCH FOUND");
  $.get("http://localhost:8080/myApp/JobSearchItem.xhtml", function (data) {
    $("#searchTextField").autocomplete({
      minLength: 2,
      source: myData,
    }).val('NO SEARCH FOUND').data('autocomplete')._trigger('select');


    $.each(data, function (k, v) {
      myData.push({
        id: v.id,
        label: v.label,
        value: v.id
      });
    });
  });
}); 
html


搜索

您好,我想您正在寻找的是,如果没有找到匹配项,它应该显示在下拉列表中。所以你需要像这样更新你的代码

$(function() {
    $("#userInput").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "http://api.stackexchange.com/2.1/users", // update with your url
                data: {
                    site: 'stackoverflow',
                    inname: request.term
                },
                dataType: 'jsonp'
            }).done(function(data) {
                if (data.items) {
                    response($.map(data.items, function(item) {
                        console.log(item);
                        return item.display_name + " " + item.location; // return your value which is coming from ajax response
                    }));
                }
            });
        },
        minLength: 1,
        response: function(event, ui) {
            if (!ui.content.length) {
                var message = { value:"",label:"No results found" };
                ui.content.push(message);
            }
        }
    });
});

<label for="userInput">Search StackOverflow user:</label>
<input id="userInput" type="text" />
$(函数(){
$(“#用户输入”).autocomplete({
来源:功能(请求、响应){
$.ajax({
url:“http://api.stackexchange.com/2.1/users“,//使用您的url更新
数据:{
站点:“stackoverflow”,
inname:request.term
},
数据类型:“jsonp”
}).完成(功能(数据){
if(数据项){
响应($.map(data.items,函数(item)){
控制台日志(项目);
return item.display_name+“”+item.location;//返回来自ajax响应的值
}));
}
});
},
最小长度:1,
响应:功能(事件、用户界面){
如果(!ui.content.length){
var message={value:,标签:“未找到结果”};
ui.content.push(消息);
}
}
});
});
搜索StackOverflow用户:

请检查工作

如果
searchTextField
input
,那么它应该是
$(“#searchTextField”).val(“未找到搜索”)如果我将代码放在上面代码中的同一位置,当没有搜索时,它将只显示为一个值,而不是自动完成下拉列表中的值。尝试
myData.push({id:0,标签:“找不到结果”})谢谢。工作起来很有魅力。:-)
$(function() {
    $("#userInput").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "http://api.stackexchange.com/2.1/users", // update with your url
                data: {
                    site: 'stackoverflow',
                    inname: request.term
                },
                dataType: 'jsonp'
            }).done(function(data) {
                if (data.items) {
                    response($.map(data.items, function(item) {
                        console.log(item);
                        return item.display_name + " " + item.location; // return your value which is coming from ajax response
                    }));
                }
            });
        },
        minLength: 1,
        response: function(event, ui) {
            if (!ui.content.length) {
                var message = { value:"",label:"No results found" };
                ui.content.push(message);
            }
        }
    });
});

<label for="userInput">Search StackOverflow user:</label>
<input id="userInput" type="text" />