[英]Unable to get value of textbox in jquery autocomplete
我使用的是jquery 1.9.1.js
和jquery-ui-1.10.3.custom.min.js
。 我在IE9瀏覽器上運行錯誤"Unable to get value of the property 'toLowerCase': object is null or undefined"
。 以下是我的代碼。
$("input[id^='TextBoxAssociate']").autocomplete(
{
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "CreateEditRequest.aspx/GetEmpNames",
data: "{'empName':'" + $(this).val() + "'}",
dataType: "json",
success: function (data) {
response($.map(data.d, function (el) {
return {
label: el.EmpName,
value: el.EmpId
};
}));
},
error: function (result) {
alert("Error");
}
});
}
當我評論response($.map(data.d, function (el) { ... }
部分然后沒有錯誤而沒有輸出。版本控制或瀏覽器兼容性可能存在問題。我也嘗試了ie8。還要檢查通過增加
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
但不適合我,並在標題上顯示上述消息。
錯誤在jquery.1.9.1.js中
val: function( value ) {
var ret, hooks, isFunction,
elem = this[0];
if ( !arguments.length ) {
if ( elem ) {
hooks = jQuery.valHooks[ elem.type ] || jQuery.valHooks[ elem.nodeName.toLowerCase() ];
if ( hooks && "get" in hooks && (ret = hooks.get( elem, "value" )) !== undefined ) {
return ret;
}
ret = elem.value;
return typeof ret === "string" ?
// handle most common string cases
ret.replace(rreturn, "") :
// handle cases where value is null/undef or number
ret == null ? "" : ret;
}
return;
}
在jQuery UI的自動完成中 , this
將不包含對相關input
的引用。 它可能會保留對新創建的函數的引用,但這是未記錄的。
要實現您的目標,您有兩種選擇:
然后, 記錄下來 ,使用request.term
(它是一個字符串):
$("input[id^='TextBoxAssociate']").autocomplete({
source: function (request, response) {
$.ajax({
// ...
data: "{'empName':'" + request.term + "'}", // <--------------------
// ...
});
});
autocomplete
在這種情況下,您必須將元素保存在.autocomplete()
調用外部的變量中。
由於"input[id^='TextBoxAssociate']"
可能會返回幾個元素,因此您必須使用.each()
循環:
$("input[id^='TextBoxAssociate']").each(function () {
var myElement = $(this);
myElement.autocomplete({
source: function (request, response) {
$.ajax({
// ...
data: "{'empName':'" + myElement.val() + "'}", // <-----------------
// ...
});
}
});
在這種方法中,其他jQuery函數,如.attr()
和else,將像往常一樣可用於myElement
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.