1

我必须在我的 html 上添加项目

一个输入文本(公司)字段和一个选择(公司列表)

当用户在文本字段中键入时,我希望 jQuery 使用 /Home/SearchSynonym/ 来获取名称并将其显示在选择中(SearchSynonym 采用值用户类型并进行通配符搜索并返回 ID 和 NAME)

有人可以帮忙吗,我是新来的 jQuery

勒布

4

2 回答 2

1

您需要澄清 SearchSynonym 以哪种格式返回 ID 和 NAME?是 JSON 格式的吗?如果是 JSON,请尝试以下操作:

var companyList = $("#Company_List");

$("#company").change( function(){
    $.getJSON("/Home/SearchSynonym/",{ query: $(this).val() }, function(response){
        var responseList = "";
        $.each(result, function(index, item){
            responseList += "<option value='" + item.id + "'>" + item.name + "</option>";
        });
        companyList.html(responseList);
    });
});    

如果您的“公司”文本字段在标签声明中包含 id="company",您的“公司列表”下拉列表在标签声明中包含 id="Company_List",并且您的服务器端接收到用于拉取的参数“查询”,这将起作用记录。

于 2013-05-13T08:39:22.610 回答
1

您可以使用 setTimeout。试试这个:

var companyList = $("#Company_List");

$("#company").change( function(){
    setTimeout( function() {
        $.getJSON("/Home/SearchSynonym/",{ query: $(this).val() }, function(response){
            var responseList = "";
            $.each(result, function(index, item){
                responseList += "<option value='" + item.id + "'>" + item.name + "</option>";
            });
            companyList.html(responseList);
        });
    }, 2000);    
});

值 2000 表示延迟 2 秒。

于 2013-05-13T10:34:09.080 回答