1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| <script type="text/javascript">
$(document).ready(function () {
//Lors de l'écriture dans le text box on récupère la liste des noms depuis la bdd
$("#<%=txtsearch.ClientID %>").keyup(function () {
var $dtt = $("#<%=txtsearch.ClientID %>").attr("value");//on récupère le nom écrit
var options = {
type: "POST",
url: "new_test.aspx/GetData",
data: "{'nom':'" + $dtt + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if ($("#<%=txtsearch.ClientID %>").attr("value") != "") {
//on vide la liste des propositions
$("#dvlist").remove();
$("#dvcontainer").append('<div id="dvlist"></div>');
//pour chaque nom retourné on crée un label
$.each(response.d, function (key, value) {
$("#dvlist").append('<label id="_' + value + '">' + value + '</label><br />');
});
//Lors du click sur un label on met sa valeur dans le text box
$("label[id^='_']").click(function () {
$("#<%=txtsearch.ClientID %>").attr("value", $(this).text());
$("#<%=txtsearch.ClientID %>").focus();
});
} else {
$("#dvlist").remove();
}
},
error: function (response) {
alert(response.status + ' ' + response.statusText + ' ' + $dtt + ' ' + response.d);
}
};
$.ajax(options);
});
});
</script>
<br />
<asp:TextBox runat="server" ID="txtsearch"></asp:TextBox><br />
<div id="dvcontainer">
</div> |
Partager