在剃须刀视图中自动完成渲染项目到动态文本框?
AutoComplete with render item to dynamic textbox in razor view?
我在 Razor 局部视图中有几个文本框,它们将根据条件可用。其中 1 使用以下代码附加到自动完成但是当加载部分视图时我得到错误(如果文本框不可用)
0x800a138f - JavaScript runtime error: Unable to set property
'_renderItem' of undefined or null reference
如果文本框在 Razor 视图中可用,则不会出现错误。
我的 Jquery 代码是
<script type="text/javascript">
$(function () {
var view = $(document).findByClass("product-view");
var companysource = view.data("datasource-url");
$("#txtcompany").autocomplete({
minLength: 0,
source: function (request, resonse) {
$.ajax({
url: companysource,
data: { term: $('#txtcompany').val() },
dataType: "json",
type: "GET",
success: function (data) {
resonse(data);
}
});
},
focus: function (event, ui) {
$("#txtcompany").val(ui.item.Name);
return false;
},
select: function (event, ui) {
$("#txtcompany").val(ui.item.Name);
return false;
},
change: function (event, ui) {
if (ui.item == null) {
} else {
}
}
})
.data("ui-autocomplete")._renderItem = function (ul, item) {
return $("<li>")
.data("ui-autocomplete-item", item)
.append("<div style='margin-bottom:2px; padding:1px 1px; font-size:14px;'><a>" + "<b>Company Name: </b>" + item.Name + "</a></div>")
.appendTo(ul);
};
});
您应该首先检查 dom 中是否存在文本框:
var txtcompany = $("#txtcompany");
alert(txtcompany.length);
if (txtcompany.length === 1) {
txtcompany.autocomplete({ ...
};
我在 Razor 局部视图中有几个文本框,它们将根据条件可用。其中 1 使用以下代码附加到自动完成但是当加载部分视图时我得到错误(如果文本框不可用)
0x800a138f - JavaScript runtime error: Unable to set property '_renderItem' of undefined or null reference
如果文本框在 Razor 视图中可用,则不会出现错误。
我的 Jquery 代码是
<script type="text/javascript">
$(function () {
var view = $(document).findByClass("product-view");
var companysource = view.data("datasource-url");
$("#txtcompany").autocomplete({
minLength: 0,
source: function (request, resonse) {
$.ajax({
url: companysource,
data: { term: $('#txtcompany').val() },
dataType: "json",
type: "GET",
success: function (data) {
resonse(data);
}
});
},
focus: function (event, ui) {
$("#txtcompany").val(ui.item.Name);
return false;
},
select: function (event, ui) {
$("#txtcompany").val(ui.item.Name);
return false;
},
change: function (event, ui) {
if (ui.item == null) {
} else {
}
}
})
.data("ui-autocomplete")._renderItem = function (ul, item) {
return $("<li>")
.data("ui-autocomplete-item", item)
.append("<div style='margin-bottom:2px; padding:1px 1px; font-size:14px;'><a>" + "<b>Company Name: </b>" + item.Name + "</a></div>")
.appendTo(ul);
};
});
您应该首先检查 dom 中是否存在文本框:
var txtcompany = $("#txtcompany");
alert(txtcompany.length);
if (txtcompany.length === 1) {
txtcompany.autocomplete({ ...
};