首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将事件发送到另一个函数

将事件发送到另一个函数
EN

Stack Overflow用户
提问于 2019-04-02 08:07:05
回答 1查看 61关注 0票数 0

问题

有没有办法用jQuery把“事件”发送给另一个函数?我有这个代码来防止删除我的表中的行,以执行某些处理,然后删除行。我想要添加一个模式窗口之间。但我不知道该怎么做。

实际上,我很喜欢这个

代码语言:javascript
复制
$('#delete-row').on('click', '.tr-special .remove-line-exceptionnel', function (event) {
    event.preventDefault();
    var $row = $(event.target).closest('.tr-special');
    console.log($row);
    var elementSortable = $row.find('ul').attr('id');
    items = $row.find('li');
    $( items ).each(function( index ) {
      //function out of purpose     
    });
    $row.remove();
});

console.log的输出是

代码语言:javascript
复制
r.fn.init [tr.tr-special, prevObject: r.fn.init(1)]

我想调用我的模式,在点击Delete按钮后使用class ..remove line,所以在正常情况下我是这样做的

代码语言:javascript
复制
$('.remove-line').on('click', function (event) {
    $('#custom-width-modal').modal('show');

});

我想要的是:当我在我的模式中按下确认按钮时,$('#delete-row').on('click','.tr-special ..remove line‘,function (event) {..执行。

代码语言:javascript
复制
$('.modal-footer').on('click', '.validDelete', function(e) {
    var $row = $(e.target).closest('.tr-special');
    console.log($row);

});

console.log的输出是

代码语言:javascript
复制
r.fn.init [prevObject: r.fn.init(1)]

我希望你能理解我

EN

回答 1

Stack Overflow用户

发布于 2019-04-02 08:41:55

如果您简单地全局声明$row变量...

.remove-line-exceptionnel单击处理程序中,存储可能要删除的元素。然后在.validDelete单击处理程序中删除该元素。

所以在任何地方都不会发送事件。有两个事件。一个是知道要删除哪个元素,另一个是实际删除。

代码语言:javascript
复制
// Declare a variable globally
var $row;

// Button (or whatever) in the row
$('#delete-row').on('click', '.tr-special .remove-line-exceptionnel', function (event) {
  // Store the row element to delete
  $row = $(event.target).closest('.tr-special');
  // Show modal
  $('#custom-width-modal').modal('show');
});

// Modal button
$('.modal-footer').on('click', '.validDelete', function(e) {
  // Remove the row
  $row.remove();
  // Hide modal
  $('#custom-width-modal').modal('hide');
});
代码语言:javascript
复制
table{
  margin: 0 auto;
}
td{
  padding: 1em;
  border: 1px solid black;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>

<table id="delete-row">
  <tr class="tr-special">
    <td>Row 1</td>
    <td><button class="remove-line-exceptionnel">Remove</button></td>
  </tr>
  <tr class="tr-special">
    <td>Row 2</td>
    <td><button class="remove-line-exceptionnel">Remove</button></td>
  </tr>
  <tr class="tr-special">
    <td>Row 3</td>
    <td><button class="remove-line-exceptionnel">Remove</button></td>
  </tr>
</table>

<div id="custom-width-modal" class="modal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Are you sure?</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary validDelete">Yes, I'm sure.</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55465163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档