<!--html-->
<tr>
<td data-id="54">
<input type="text" class="suggestexam" />
</td>
<tr>
<tr>
<td data-id="16">
<input type="text" class="suggestexam" />
</td>
<tr>
//JQuery
$(".suggestexam").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Employee/GetExamNameSuggestion",
data: "{'prefix':'" + request.term + "','testval':'"+ $(this).closest('td').attr('data-id') +"'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}
});
但是$(this).closest('td').attr('data-id')
返回undefined。
任何帮助?
答案 0 :(得分:2)
<div data-id="54">
<input type="text" name="tag" class="autosuggest" id="tagid" />
</div>
<script>
$(function () {
$('body').on('focus', ".autosuggest", function () {
var autosuggestType = $(this).closest('div').attr("data-id");
$(this).autocomplete({
source: function (request, response) {
$.ajax({
url: "/Home/FetchTagList",
data: JSON.stringify({ tag: request.term, exid: autosuggestType }),
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
console.log(data.data);
response($.map(data.data, function (item) {
return {
value: item.TagName
}
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
minLength: 1
});
});
//$(".autosuggest").each(function (index, object) { });
});
</script>
答案 1 :(得分:0)
请参阅this post。试试这个:
//JQuery
var dataId = $(".suggestexam").closest('td').attr('data-id');
$(".suggestexam").autocomplete({
source: function(request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Employee/GetExamNameSuggestion",
data: "{'prefix':'" + request.term + "','testval':'" + dataId + "'}",
dataType: "json",
success: function(data) {
response(data.d);
},
error: function(result) {
alert("Error");
}
});
}
});
答案 2 :(得分:0)
$('input').focus(function() {
console.log($(this).parent().closest('td').data('id'))
$(this).val($(this).parent().closest('td').data('id'));
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tr>
<td data-id="54">
<input type="text" class="suggestexam" />
</td>
</tr>
</table>
&#13;
试试这个