أماني مفيد

أماني مفيد

  • NA
  • 142
  • 17.2k

Autocomplete Text box Ajax

Sep 6 2020 2:12 AM
i have bellow script fro autocomplete tex box , the fucntion in controller take parameter (prifix ) 
the script return all data how i can pass the prifix from ajax to controller for example when i type (a) it retuen all data start or not statrt with (a)
this is the script 
<head>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<input id='CountryName'/>
<span id='CountryID'></span>
<script>
//var data2 = ["Ahmad Hoshya", "Amani Mustafa", "Maamoun Zakarneh", "Sabeel"];
var data2;
//var firstname = $('#CountryName').val();
var firstname = document.getElementById("CountryName").value;
$.ajax({
url: '/Home/GetRecord?prefix='+ firstname,
dataType: "json",
type : "post" ,
data: data2,
success: function (data) {
data2 = data;
},
error: function (xhr, status, error) {
alert("Error");
}
});
$('#CountryName').autocomplete({
data: { search: $("#searchInput").val() },
source: function y(request, response) {
response($.map(data2, function (item) {
return {
label: item.t,
value: item.t
}
}));
}
,
select: function (e, ui) {
e.preventDefault();
$(this).val(ui.item.label);
$('#CountryID').html(ui.item.value);
}
});
</script>
</body>

Answers (2)