0

我为 Jquery Auto 完成了很多研发工作,我找到了一些结果,但没有我需要的那么多。我给你的代码是我目前正在使用的。

    <ul>
         @foreach (var items in Model)
            {
             <li class="@items.ShortDesc" id="@items.ShortDesc">
                <div class="test0">             
                @Html.TextBox(@items.ShortDesc, @items.SfldDefault, new { @class = "catalog inputcatalog txtbox" })          
                                </div>  
                        </li>

                    }
                </ul> This  input  box will  created  by  Dynamic using forloop

//我的jQuery代码

     $(function () {
    $("#Subject").autocomplete({
        source: '/Cataloging/Bib/GetSubject',
        minLength: 1,
        select: function (event, ui) {
            // Do something with "ui.item.Id" or "ui.item.Name" or any of the other properties you selected to return from the action
        }
    });
});   

// 我的动作方法

  public ActionResult GetSubject(string term)
    {
         term = term.Substring(2, term.Length-2);

        return Json(db.BibContents.Where(city => city.Value.StartsWith(term)).Select(city => city.Value), JsonRequestBehavior.AllowGet);
    }

// 我的代码使用静态输入运行,但是在创建动态时我需要使用实时事件,但我不知道如何使用此代码使用实时事件。

注意:我在渲染操作后使用输入“|a”的静态值,我正在删除前两个字符以从数据库中进行正确搜索。谢谢

4

2 回答 2

1

更改此项以为所有 txtboxes 创建相同的类

    <li class="ShortDesc" id="@items.ShortDesc">

.

$(document).ready(function () { 
    $('.ShortDesc').each(function () { 
            $(this).autocomplete({ 
                source: '/Cataloging/Bib/GetSubject',
                minLength: 1,
                select: function (event, ui) {
            // Do something with "ui.item.Id" or "ui.item.Name" or any of the other properties you selected to return from the action
        }
    });
});

$(Document).ready()应该在所有 Razor 命令完成后发生

于 2012-11-20T04:48:09.910 回答
0

每次动态创建输入后调用一个函数

 create_dynaically(id);

这个功能

function create_dynaically(id)
{

$("#"+id).autocomplete({   //this line
        source: '/Cataloging/Bib/GetSubject',
        minLength: 1,
        select: function (event, ui) {
            // Do something with "ui.item.Id" or "ui.item.Name" or any of the other properties you selected to return from the action
        }
    });

} 
于 2012-11-20T04:52:17.543 回答