javascript - Twitter Bootstrap 输入标签在()之前不适用于 Jquery

标签 javascript jquery html css twitter-bootstrap

我正在使用 Jquery 来 clone() 一个 div 并更改它的 child 的 id,其中一个 child 是 Bootstrap-tagsinput .

你可以找到一个Demo here .

点击添加新运行后,添加了一个新的div,但标签输入是不可编辑的!!

这是我的代码(您可以查看完整代码 here ):

$('#addrun').click(function () {

                var s = $('#run1')
                    .clone()
                    .attr('id', '#run' + (++runNum))
                    .wrap('<div>');



                s.find('#tag1').attr('id', 'tag2');
                $('#tag2').tagsinput();
                $('#addrun').before(s.parent().html());

                $(".well").on('click', '.expandbtn', function () {
                    var $this = $(this).parent();
                    var $collapse = $this.closest('.RunWell').find('.SystemFiles');
                    $collapse.collapse('toggle');
                });
                $('.SystemFiles').collapse('collapse');

            });

最佳答案

#tag2 添加到页面后,尝试调用 .tagsinput()

$('#addrun').before(s.parent().html());
$('#tag2').tagsinput();

编辑:

这可能是由于 TagsInput 插件如何初始化自身。我要做的是创建一个空运行容器的模板并将其隐藏在页面上或通过 JavaScript 加载它。

<div class="control-group hide" id="ControlGroupTemplate">
  <label class="control-label" for="textarea">Tools :</label>
  <input type="text" class="tags" id="tag1" value="Amsterdam,Washington,Sydney,Beijing"
    data-role="tagsinput" />
  <br />
  <div class="SystemFiles" data-role="collapsible">
    <!-- File Button -->
    <div class="control-group">
      <label class="control-label" for="filebutton">OP Customer DLIS files (PUC)</label>
      <div class="controls">
        <input id="filebutton" name="filebutton" class="input-file" type="file">
      </div>
    </div>

    <!-- File Button -->
    <div class="control-group">
      <label class="control-label" for="filebutton">OP logup DLIS files (LUP)</label>
      <div class="controls">
        <input id="file1" name="filebutton" class="input-file" type="file">
      </div>
    </div>

    <!-- File Button -->
    <div class="control-group">
      <label class="control-label" for="filebutton">OP Producer DLIS files (PUP)</label>
      <div class="controls">
        <input id="file2" name="filebutton" class="input-file" type="file">
      </div>
    </div>

    <!-- File Button -->
    <div class="control-group">
      <label class="control-label" for="filebutton">OP well folder</label>
      <div class="controls">
        <input id="file3" name="filebutton" class="input-file" type="file">
      </div>
    </div>

    <div class="control-group">
      <label class="control-label" for="filebutton">Prints</label>
      <div class="controls">
        <input id="file4" name="filebutton" class="input-file" type="file">
      </div>
    </div>
  </div>
  <button class="btn btn-mini link-btn expandbtn" id="exp" type="button">expand toggle</button>
  <button class="btn btn-mini btn-primary"><span class="glyphicon glyphicon-arrow-down">
  </span>Duplicate</button>
</div>

然后克隆 ControlGroupTemplate 并对其应用 TagsInput 方法。

var s = $('#ControlGroupTemplate')
  .clone()
  .attr('id', '#run' + (++runNum))
  .wrap('<div>');

s.find('#tag1').attr('id', 'tag2');
$('#addrun').before(s.parent().html());
$('#tag2').tagsinput();

我什至会使用此方法将您的初始运行添加到您的 document.ready() 处理程序中的页面。

关于javascript - Twitter Bootstrap 输入标签在()之前不适用于 Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19275570/

相关文章:

javascript - NodeJS + Mongo - 如何与 DOM 交互以从数据库获取数据?

javascript - 更改参数值后刷新对象标签

javascript - 在 AJAX Success 中超时 HTML 标记

javascript - 如何从 Python 脚本中执行整个 Javascript 程序

javascript - 如何在 C3.js 中设置 X 轴刻度值的格式

javascript - jQuery UI 可调整大小 : set size programmatically?

jQuery - 将图像包含在具有动态设置宽度的 div 中

javascript - 使用 jquery 选择选项卡

javascript - 如何获取对div的引用?

ios - 输入类型数字在 iOS 中不接受十进制