2016-06-30 27 views
1

私は準備が整えられたコードを使用してプロジェクトで実装しています。コードが1つのテキストボックスでうまく動作し、出力も得られるようになりました。別のテキストボックスに使用すると、サーバー側から結果が得られましたが、何とかクライアントサイドに表示されません。オートコンプリートのテキストボックスデザインの不一致

@model IEnumerable<UMS.Models.UserDetail> 
.... 
<script src="~/Scripts/jquery-1.10.2.js"></script> 
<script src="~/Scripts/jquery-ui-1.9.0.min.js"></script> 
.... 
@using (Html.BeginForm("Index", "UserDetails", FormMethod.Get)) 
{ 
    @Html.TextBox("SearchName", "", new { @class = "control-label col-md-2", Style = "margin-right:10px;", placeholder = "Name", id = "txtUserName" }) 
    @Html.TextBox("SearchEmail", "", new { @class = "control-label col-md-2", Style = "margin-right:10px;", placeholder = "Email", id = "txtEmail" }) 
    @Html.DropDownList("SearchDesignation", ViewBag.DesignationList as SelectList, "Select", new { @class = "control-label col-md-2", Style = "margin-right: 10px; margin-top: 5px; height: 32px;" }) <input type="submit" value="Search" onclick="location.href='@Url.Action("Index", "Users")'" /> 
} 
<table class="table"> 
    .... 
</table> 

<script type="text/javascript"> 
    $("#txtUserName").keypress(function() { 
     $("#txtUserName").autocomplete({ 
      source: function (request, response) { 
       $.ajax({ 
        url: "/UserDetails/SearchName", 
        type: "POST", 
        dataType: "json", 
        data: { Prefix: request.term }, 
        success: function (data) { 
         response($.map(data, function (item) { 
          return { label: item.Name, value: item.Name }; 
         })) 
        } 
       }) 
      }, 
      messages: { 
       noResults: "", results: "" 
      } 
     }); 
    }) 

    $("#txtEmail").keypress(function() { 
     $("#txtEmail").autocomplete({ 
      source: function (request, response) { 
       $.ajax({ 
        url: "/UserDetails/SearchEmail", 
        type: "POST", 
        dataType: "json", 
        data: { Prefix: request.term }, 
        success: function (data) { 
         response($.map(data, function (item) { 
          return { label: item.Name, value: item.Name }; 
         })) 
        } 
       }) 
      }, 
      messages: { 
       noResults: "", results: "" 
      } 
     }); 
    }) 
</script> 

上記は私のコードです。ここで私は2つの画像を添付しています。他の唯一のドットで表示enter image description hereenter image description here

+0

である必要がありますか? –

+0

いいえ、動的なUIタグをHTMLで生成します – Developer

+0

コンソールに何かエラーがありますか? –

答えて

-1

いる間に一枚の画像では、あなたは、あなたが複数回発生するイベントで初期化コードを書くべきではありません成功

$(document).unbind('keypress'); 
+0

このコードを次のように成功させる必要がありますか?success:function(data){ response($。map(data、function(item){$(document).unbind( 'keypress '); return {label:item.Name、value:item.Name}; })) } – Developer

0

後にそれをテストすることができ、結果を見ることができます。イベントが発生するたびに、ウィジェットの動作をさせるのではなく、ウィジェットを再初期化するだけです。あなたのコードは:

$("#txtUserName").autocomplete({ 
    source: function(request, response) { 
    $.ajax({ 
     url: "/UserDetails/SearchName", 
     type: "POST", 
     dataType: "json", 
     data: { 
     Prefix: request.term 
     }, 
     success: function(data) { 
     response($.map(data, function(item) { 
      return { 
      label: item.Name, 
      value: item.Name 
      }; 
     })) 
     } 
    }) 
    }, 
    messages: { 
    noResults: "", 
    results: "" 
    } 
}); 
+0

解決に感謝します。しかし、2番目の関数、つまりtxtEmail関数にエラーがあります。私はあなたのコードを実装していたが、私はまだそのエラーに直面している。 – Developer

+0

エラーの詳細をお知らせしない限り、私たちはあなたを助けません。 –

+0

私はすでに私の質問のエラーについて言及しています。 – Developer