【问题标题】:Find closes element on input change and show tooltip在输入更改时查找最近的元素并显示工具提示
【发布时间】:2017-02-06 23:31:38
【问题描述】:

当用户更改输入 name="qty" 并在该元素上显示引导工具提示时,我需要找到最接近的元素 fa-retweet...

我尝试使用代码:

$('input[name=qty]').change(function() { 
  $(this).closest('.fa-retweet').tooltip('show'); 
 });

但什么也没发生。请帮忙。

使用完整的 html 代码更新:

<form method="post" action="/updateCart/35c3d0bd350dba471e8270d0f61e559d" accept-charset="UTF-8">
<input name="_method" type="hidden" value="post">
<input type="hidden" name="_token" value="3r2qqrgYG62HVB5xkOOJYN5lVm5v4TNWK5ECQcoH"> 
<div class="col-md-1 col-xs-4" style="padding:0px; ">
  <input class="margin-mobile" type="number" name="qty" id="qty" value="3" style="width:40px; height: 30px; margin-top:5px; text-align: center !important; border: 1px solid #cf931e; color: #cf931e;">
</div>
<div class="col-md-2 col-xs-4 text-right" style="padding:0px;">
  <h2 class="text-theme" style="font-size:36px !important; margin-top:0px !important;">£96    
<sup style="position: relative;vertical-align: baseline;line-height: 0;font-size: 14px;top: -1.2em;">
00  </sup>  </h2>
</div>
<div class="col-md-2 col-xs-4 text-right" style="margin-top:5px;">
  <h3 style="margin:0px !important;"><button type="submit" data-toggle="tooltip" title="Click here to change quantity!" class="text-theme btn-non"><i style="margin-right:-5px;" class="fa fa-retweet" aria-hidden="true"></i></button> | <a href="/remove/35c3d0bd350dba471e8270d0f61e559d" class="text-theme"><i class="fa fa-trash" aria-hidden="true"></i></a></h3>
</div>
</form>

【问题讨论】:

  • 尝试将qty 放在引号中:$("input[name='qty']")。另外,您是否遇到任何错误?
  • 不,没有错误
  • 完整代码在哪里?
  • 我用完整代码更新

标签: jquery html twitter-bootstrap onchange closest


【解决方案1】:

.closest() 搜索DOM 树,直到找到与提供的选择器匹配的匹配项,但.fa-retweet 不在这条路上。

这将起作用:$(this).closest('form').find('.fa-retweet').tooltip('show');

UPD。docs中所述:

工具提示所需的标记只是 data 属性和 title 在您希望有一个工具提示的 HTML 元素上。

但是在您的变体标签&lt;i> 中没有数据和标题,但您在&lt;button&gt; 中有这个。所以就这样吧:

$(this).closest('form').find('button').tooltip('show');

或查看示例代码:jsfiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    相关资源
    最近更新 更多