jQuery ajax 自动完成没有为隐藏字段赋值
jQuery ajax autocomplete not assigning value to hiddenfield
jQuery 自动完成有一个非常奇怪的问题,请检查下面的脚本代码
<script src="Scripts/jquery1.4.min.js" type="text/javascript"></script>
<script src="Scripts/jquery1.8-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("[id$=txtSearch]").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/SearchResultsWS.asmx/GetFacilityNames") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('*----*')[0],
val: item.split('*-----*')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("[id$=hfFacilityId]").val(i.item.val);
//$("[id$=valueText]").text(i.item.val);
alert(i.item.val);
//return false;
},
minLength: 2
});
});
$(document).ready(function () {
$('input[type=text]').click(function () {
$(this).select();
});
});
</script>
这是asp.net代码
<label for="txtSearch" id="SearchLabel" runat="server">Search Reports: </label>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:HiddenField ID="hfFacilityId" runat="server" />
<asp:TextBox ID="valueText" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Go!" OnClick="Submit"/>
我正在使用 Web 服务从 SQL 服务器数据库加载自动完成列表,一切正常,除了 $("[id$=hfFacilityId]").val(i.item.val);
和 alert(i.item.val);
语句。
没有分配给隐藏字段和警报抛出 "undefined"!!
我试图解决这个问题但没有成功。如果我使用 i.Item.label 而不是 i.item.val 它工作正常。所以与 item.val 有关的东西不起作用。
感谢任何帮助..提前致谢
在下面的代码中,与 label: item.split('*----*')
相比,val: item.split('*-----*')
中有额外的破折号 -
return {
label: item.split('*----*')[0],
val: item.split('*-----*')[1]
}
jQuery 自动完成有一个非常奇怪的问题,请检查下面的脚本代码
<script src="Scripts/jquery1.4.min.js" type="text/javascript"></script>
<script src="Scripts/jquery1.8-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("[id$=txtSearch]").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/SearchResultsWS.asmx/GetFacilityNames") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('*----*')[0],
val: item.split('*-----*')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("[id$=hfFacilityId]").val(i.item.val);
//$("[id$=valueText]").text(i.item.val);
alert(i.item.val);
//return false;
},
minLength: 2
});
});
$(document).ready(function () {
$('input[type=text]').click(function () {
$(this).select();
});
});
</script>
这是asp.net代码
<label for="txtSearch" id="SearchLabel" runat="server">Search Reports: </label>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:HiddenField ID="hfFacilityId" runat="server" />
<asp:TextBox ID="valueText" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Go!" OnClick="Submit"/>
我正在使用 Web 服务从 SQL 服务器数据库加载自动完成列表,一切正常,除了 $("[id$=hfFacilityId]").val(i.item.val);
和 alert(i.item.val);
语句。
没有分配给隐藏字段和警报抛出 "undefined"!!
我试图解决这个问题但没有成功。如果我使用 i.Item.label 而不是 i.item.val 它工作正常。所以与 item.val 有关的东西不起作用。
感谢任何帮助..提前致谢
在下面的代码中,与 label: item.split('*----*')
val: item.split('*-----*')
中有额外的破折号 -
return {
label: item.split('*----*')[0],
val: item.split('*-----*')[1]
}