我试图访问td
函数中的coolautosuggest
值,但是它正在undefined value
。
HTML CODE:
<table class="table table-bordered cust_job_over">
<thead>
<tr>
<th>Skillset</th>
<th>Assignee</th>
</tr>
</thead>
<tbody class="LabourTbody">
<tr>
<td width="45%">
<input type="hidden" class="labourAssetID" value="31" name=""><span class="skill">0.35m Planer Operator</span></td>
<td width="40%" style="position: relative;">
<input type="hidden" class="" value="167,169,172,173" name="">
<input type="text" class="form-control cool-auto-suggest" style="width: 90% !important;float:left;">
<div class="assig_noti assign_complete cust_job_over_noti"></div>
</td>
</tr>
<tr>
<td width="45%">
<input type="hidden" class="labourAssetID" value="33" name=""><span class="skill">Sweeper Driver (CE)</span></td>
<td width="40%" style="position: relative;">
<input type="hidden" class="" value="167,169,172,173" name="">
<input type="text" class="form-control cool-auto-suggest" style="width: 90% !important;float:left;">
<div class="assig_noti assign_complete cust_job_over_noti"></div>
</td>
</tr>
</tbody>
</table>
JQUERY CODE:
$(document).ready(function() {
$(".cool-auto-suggest").coolautosuggest({
url: window.location.origin + "/datalist/",
showThumbnail: true,
showDescription: true,
additionalFields: {
"/asset": $(this).closest('tr').find('td:eq(0) .labourAssetID')
},
onSelected: function(result) {
}
});
});
这是我获取未定义值"/asset": $(this).closest('tr').find('td:eq(0) .labourAssetID')
This is the plugin 我试图实施。
有人可以建议我如何在此功能中使用td
获取最接近的class name
输入值吗?
答案 0 :(得分:3)
问题是$(this)
引用了文档对象,因为您位于$(document).ready()
事件的上下文中。
如果您有多个cool-auto-suggest
类,则需要迭代每个元素:
$(document).ready(function() {
$(".cool-auto-suggest").each(function() {
// Here I save reference to the current iterated object
var $el = $(this);
// Here I calling the plugin separately, for each '.cool-auto-suggest'
$el.coolautosuggest({
url: window.location.origin + "/datalist/",
showThumbnail: true,
showDescription: true,
additionalFields: {
// Here I select the correct element using the reference ($el)
"/asset": $el.closest('tr').find('td:eq(0) .labourAssetID')
},
onSelected: function(result) {
}
});
});
});