问题
有没有办法用jQuery把“事件”发送给另一个函数?我有这个代码来防止删除我的表中的行,以执行某些处理,然后删除行。我想要添加一个模式窗口之间。但我不知道该怎么做。
实际上,我很喜欢这个
$('#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的输出是
r.fn.init [tr.tr-special, prevObject: r.fn.init(1)]我想调用我的模式,在点击Delete按钮后使用class ..remove line,所以在正常情况下我是这样做的
$('.remove-line').on('click', function (event) {
$('#custom-width-modal').modal('show');
});我想要的是:当我在我的模式中按下确认按钮时,$('#delete-row').on('click','.tr-special ..remove line‘,function (event) {..执行。
$('.modal-footer').on('click', '.validDelete', function(e) {
var $row = $(e.target).closest('.tr-special');
console.log($row);
});console.log的输出是
r.fn.init [prevObject: r.fn.init(1)]我希望你能理解我
发布于 2019-04-02 08:41:55
如果您简单地全局声明$row变量...
在.remove-line-exceptionnel单击处理程序中,存储可能要删除的元素。然后在.validDelete单击处理程序中删除该元素。
所以在任何地方都不会发送事件。有两个事件。一个是知道要删除哪个元素,另一个是实际删除。
// 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');
});table{
margin: 0 auto;
}
td{
padding: 1em;
border: 1px solid black;
}<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">×</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>
https://stackoverflow.com/questions/55465163
复制相似问题