【问题标题】:Control the confirm button in bootstrap confirm modal在引导程序确认模式中控制确认按钮
【发布时间】:2016-01-12 13:06:23
【问题描述】:

Confirm modal example

我把这个例子改成了一个简单的例子。当我单击“删除此 {{item.id}}”按钮时,我想调用删除函数。标题成功获取item.id值。

<h4 class="modal-title" id="exampleModalLabel">Do you want to remove  {{item.id}}</h4>

但是按钮没有获取 item.id 值并且该功能不起作用。而不是“Remove this item.id”,而只是“Remove this”,并且函数也没有获取参数。

<button type="button" id="exampleModalLabel" class="btn btn-primary" ng-click="removeItem(item.id)">Remove this {{item.id}}</button>

我拥有的是这样的:

<tr ng-repeat="item in items">
    <td>{{item.id}}</td>
    <td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="{{item.id}}">Remove this item?</button></td>
</tr>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="exampleModalLabel">Do you want to remove  {{item.id}}</h4>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" ng-click="removeItem(item)">Remove this {{item.id}}</button>
      </div>
    </div>
  </div>
</div>

//And this javascript
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var recipient = button.data('whatever');
var modal = $(this);
modal.find('.modal-title').text('New message to ' + recipient);
modal.find('.modal-body input').val(recipient);
});

我希望信息足够。如果您需要更多信息,请告诉我。

【问题讨论】:

  • 不应该 removeItem(item.id) 是 removeItem({{ item.id }}) 吗?
  • 不,同样的结果......它甚至没有在innerHTML上得到它......
  • Modal 是否出现在屏幕上?
  • 您始终可以通过 javascript 设置值来解决问题。在 JS 中设置值: var item = '{{item.id}}';然后在您的模态函数中: modal.find('.modal-body button').attr("ng-click", "removeItem("+item+")");
  • @PaulMoldovan 我试过了,但还是不行……

标签: javascript jquery angularjs twitter-bootstrap bootstrap-modal


【解决方案1】:

试试这个:

<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="button" id="removeButton" class="btn btn-primary">Remove this <span id="itemid"></span></button>
</div>

//JS
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var recipient = button.data('whatever');
var modal = $(this);
modal.find('.modal-title').text('New message to ' + recipient);
modal.find('.modal-body input').val(recipient);

modal.find('#itemid').html(recipient); // add this
modal.find('#removeButton').attr('ng-click', 'removeItem('+recipient+')'); // add this
});

【讨论】:

  • 一切似乎都很好,但函数不想触发......我不知道为什么......我正在尝试调试它,看看出了什么问题,但似乎就像函数只是没有触发......
  • 的那个工作正常......它甚至添加了“ng-click="removeItem(myItem)”,但是当我点击时,该功能不会触发.. .
  • 很抱歉听到这个消息。不幸的是,我不知道 AngularJs,我无法帮助你。这是一个可能有帮助的链接:stackoverflow.com/questions/27897175/…
【解决方案2】:

您的模态超出了items 范围。您需要将 item 分配给控制器内的某个临时变量。您应该使用ng-click 来实现这一点,类似于ng-click="tempItem = item"。您可能还必须编辑您的 removeItem 函数。

<tr ng-repeat="item in items">
    <td>{{item.id}}</td>
    <td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="{{item.id}}" ng-click="tempItem = item">Remove this item?</button></td>
</tr>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="exampleModalLabel">Do you want to remove  {{tempItem.id}}</h4>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" id="exampleModalLabel" class="btn btn-primary" ng-click="removeItem(tempItem)">Remove this {{tempItem.id}}</button>
      </div>
    </div>
  </div>
</div>

//And this javascript
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var recipient = button.data('whatever');
var modal = $(this);
modal.find('.modal-title').text('New message to ' + recipient);
modal.find('.modal-body input').val(recipient);
});

【讨论】:

  • 即使 modal 没有项目,它仍然设法在 &lt;h4 class="modal-title" id="exampleModalLabel"&gt;Do you want to remove {{item.id}}&lt;/h4&gt; 中获取 item.id 为什么它没有在 &lt;button type="button" id="exampleModalLabel" class="btn btn-primary" ng-click="removeItem(item)"&gt;Remove this {{item.id}}&lt;/button&gt; 中获取它
  • 我认为它无法在h4 中获得item.id,你只是用jquery modal.find('.modal-title').text('New message to ' + recipient); 更改h4 内容
  • 其实是的,我的错!
猜你喜欢
  • 2019-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 2012-11-01
相关资源
最近更新 更多