【问题标题】:Why does the title of my Bootstrap tooltip only change once with the keydown event in jQuery?为什么我的 Bootstrap 工具提示的标题只会随着 jQuery 中的 keydown 事件而改变一次?
【发布时间】:2016-03-31 09:02:57
【问题描述】:

我有文本输入和文本区域,我想向用户显示他们使用了多少字符超出了限制。因此,我为每个具有 data-maxlength 属性的元素添加了一个工具提示。每次按下一个键时,我都会为当前文本输入创建包含长度和最大长度值(x / 250 个字符)的工具提示。问题是在您第一次停止输入后,工具提示的 title 属性的值永远不会改变。这是一个jsFiddle 向您展示。

jQuery

$(document).ready(function(){
    $('[data-maxlength]').each(function(){
        $(this).on('keyup', function(){
            $(this).tooltip({
                selector: 'data-toggle="tooltip"',
                placement: 'bottom',
                trigger: 'manual',
                title: $(this).val().length + ' / ' + $(this).data('maxlength') + ' characters used.'
            });
            $(this).tooltip('show');
        });
    });
});

HTML:

<textarea name="description" data-maxlength="250"></textarea>

感谢您的宝贵时间。

【问题讨论】:

  • 请注意:当您跟踪输入更改时,最好使用input 事件而不是keyup,如果您仍然按下某个键,您会看到工具提示标题不会正确更改。
  • @ZakariaAcharki:这在这里不起作用,因为 OP 希望向用户显示在输入字段中输入/删除的每个字符的字符限制,这只有通过 keyup 事件才有可能为change 事件仅在焦点移出当前字段时触发。
  • 不确定你的意思@dreamweiver,但确定它会起作用,jsfiddle.net/vw49te7d/7
  • @ZakariaAcharki:我的错误兄弟,我误解了input 事件为change。你的建议是有效的:)

标签: javascript jquery twitter-bootstrap


【解决方案1】:

标题只在第一个关键事件上设置,试试这个:

title: function() {return $('[data-maxlength]').val().length + ' / ' + $(this).data('maxlength') + ' characters used.';}

见:How do JavaScript closures work?

我同意 Dreamveivers 的评论,所以这将是一个更好的实现:

$(document).ready(function(){
    $('[data-maxlength]').each(function(){
      var maxText = ' / ' + $(this).data('maxlength') + ' characters used.';
      var tooltip = $(this).tooltip({
            selector: 'data-toggle="tooltip"',
            placement: 'bottom',
            trigger: 'manual'
      });
      $(this).on('keyup', function(){
          tooltip.attr('title', $(this).val().length + maxText)
              .tooltip('fixTitle')
              .tooltip('show');
      });
    });
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<textarea name="description" data-maxlength="250"></textarea>

见:Change Twitter Bootstrap Tooltip content on click

【讨论】:

  • 这是一个不错的解决方案,但您不认为为同一元素上的每个 keyup 事件初始化 tooltip 会产生性能问题吗?只是更新各自的 attr 持有该标题对我来说似乎更合理
【解决方案2】:

这似乎解决了您的问题:

$(document).ready(function(){

    $('[data-maxlength]').each(function(){
        $(this).on('keyup', function(){
            $(this).attr('data-original-title', $(this).val().length + ' / ' + $(this).data('maxlength') + ' characters used.');

            $(this)
              .tooltip({
                selector: 'data-toggle="tooltip"',
                placement: 'bottom'
            })
            .data('placement', 'bottom');

            $(this).tooltip('show');
        });
    });
});

https://jsfiddle.net/vw49te7d/

在这里找到:Change Twitter Bootstrap Tooltip content on click

【讨论】:

    【解决方案3】:

    实际上,这是一个有效的问题,它已经在 github issue 日志中,到目前为止,过去 2 年多以来还没有任何重大更新。

    Github issue page

    解决方法:

    1. .each()之外的所有文本区域初始化tooltip,因为不需要通过类选择器txtarea循环遍历所有元素[在性能方面类选择器的使用远优于属性选择器] .
    2. 更新attr,其中包含tooltip 内部tooltip 回调的title 信息

    $(this).attr('data-original-title', $(this).val().length + ' / ' + $(this).data('maxlength') + ' characters used.');

    Live Demo @ JSFiddle

    【讨论】:

      【解决方案4】:

      我注意到每次我将.tooltip() 用于元素(例如textarea)时,当显示工具提示时,总会有一个&lt;div&gt; 作为元素的下一个兄弟元素(例如textarea)附加。附加的&lt;div&gt; 具有类tooltip。在&lt;div&gt; 内部有一个类tooltip-inner 的元素,其中包含作为工具提示中显示的消息的文本。以下是附加&lt;div&gt; 的示例:

      <div style="top: 63px; left: 9.5px; display: block;" id="tooltip59349" class="tooltip fade bottom in" role="tooltip">
        <div style="left: 50%;" class="tooltip-arrow"></div>
        <div class="tooltip-inner">
          <span class="n-char">0</span> / 250 characters used.
        </div>
      </div>
      

      所以,我的解决方案是替换 .tooltip-inner 中的文本。在此解决方案中,您不必每次要更改标题时都调用 .tooltip('show') 。您不会在每个按键上看到闪烁的工具提示。

      $(document).ready(function(){
          $('[data-maxlength]').each(function(){
            $(this).tooltip({
              placement: 'bottom',
              trigger: 'focus',
              html: true,
              title: '<span class="n-char">' + $(this).val().length + '</span> / ' + $(this).data('maxlength') + ' characters used.'
            });
      
            $(this).on('focus keyup', function(){
              $(this).next('.tooltip').find('.tooltip-inner .n-char').text($(this).val().length);
            });
          });
      });
      

      https://jsfiddle.net/mikhsan/okpLzg5c/

      【讨论】:

        猜你喜欢
        • 2017-12-03
        • 1970-01-01
        • 2014-10-14
        • 2017-07-22
        • 2023-03-16
        • 2020-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多