我厌倦了以下javascript来获取按钮父TD,但我没有找到它工作
$(document).on('click','.editable-submit',function(){
alert($(this).closest('span').html());
});
以上代码警告'undefined',以下是标记。在此标记中,'editable-submit'是目标。我需要的是javascript应警告< td>此示例中的值为“输出内容”。
<td>
<span class="xedit editable editable-click editable-open" data-original-title="" title="">
Output Content
</span>
<div class="popover fade top in editable-container editable-popup" style="top: 12px; left: 0px; display: block;">
<div class="arrow" style="left: 23.007246376811597%;">
</div>
<h3 class="popover-title" style="display: none;">
</h3>
<div class="popover-content">
<div>
<div class="editableform-loading" style="display: none;">
</div>
<form class="form-inline editableform" style="">
<div class="control-group form-group">
<div>
<div class="editable-input" style="position: relative;">
<input type="text" class="form-control input-sm" style="padding-right: 24px;">
<span class="editable-clear-x">
</span>
</div>
<div class="editable-buttons">
<button type="submit" class="btn btn-primary btn-sm editable-submit">
<i class="glyphicon glyphicon-ok">
</i>
</button>
<button type="button" class="btn btn-default btn-sm editable-cancel">
<i class="glyphicon glyphicon-remove">
</i>
</button>
</div>
</div>
<div class="editable-error-block help-block" style="display: none;">
</div>
</div>
</form>
</div>
</div>
答案 0 :(得分:1)
在您的代码中,span
不是单击按钮的祖先,因此您无法直接使用nearest(),您需要使用td
找到祖先closest('td')
元素然后查找span
子项使用find(),如find('span')
alert($(this).closest('td').children('span').html());