0

Jqueryオートコンプリートの研究開発をたくさん行ったので、いくつかの結果が見つかりましたが、必要なほどではありませんでした。現在使用しているコードを提供しています。

    <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"の静的な値を使用しています。データベースから適切な検索を行うために、最初の2文字を削除しています。ありがとう

4

2 に答える 2

1

これを変更して、すべてのtxtboxで同じクラスを作成します

    <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 に答える