因为我是jQuery的新手,所以我在小任务上面临困难。我有一个表,其中每个表行都有一个图标,单击该图标,将打开一个包含一些单词的模式框。当我关闭该模式框时,我想收集针对该模式框出现的表格行。
例如:
table
row1 +
row2 +
row3 +
在单击row2 +按钮时,它会打开模式框,在关闭该模式框时,我希望针对该模式框打开的行在这种情况下为2。
我写了下面的代码。
模式框:
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Choose words. You can add words, delete words </h4>
</div>
<div class="modal-body">
<!-- <input type="text" value="Amsterdam,Washington,Sydney,Beijing,Cairo" data-role="tagsinput" /> -->
<div class="modal-body-inner"></div>
</div>
<div class="modal-footer">
<button type="button" id = "modelformbuttonclick" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
表格行:
<table>
<tr id="1">
<td><td>
<td><span id="modelbox" data-target="#myModal" href="#myModal" data-toggle="modal" class="glyphicon glyphicon-plus"></span></td>
</td>
<tr id="2">
<td><td>
<td><span id="modelbox" data-target="#myModal" href="#myModal" data-toggle="modal" class="glyphicon glyphicon-plus"></span></td>
</td>
<tr id="3">
<td><td>
<td><span id="modelbox" data-target="#myModal" href="#myModal" data-toggle="modal" class="glyphicon glyphicon-plus"></span></td>
</td>
</table>
单击模式框时,它会使用jQuery由动态值填充。
$('tr #modelbox').click(function() {
var $row = $(this).closest('tr');
var tbid = $row.attr('id'); // table row ID
var fieldOption = []
$row.find('#words option').each(function() { fieldOption.push($(this).val()); });
console.log(fieldOption);
$('.modal-body-inner').html('');
for(var i = 0, size = fieldOption.length; i < size ; i++){
var item = fieldOption[i];
$('.modal-body-inner').append("<span class=" + "span1" + " id = "+ tbid +"> "+ item + "</span>");
}
});
然后在单击“关闭”按钮时我想要获得行,我得到了这样的行,但它不起作用,
$('#modelformbuttonclick').click(function() {
console.log($(this).closest('tr').data('id'));
console.log($(event.target).closest('tr').data('id'));
});
因此,希望您能理解我的问题,对此的任何帮助将不胜感激。
答案 0 :(得分:1)
在点击事件范围之外使用变量:
var tbid;
$('tr #modelbox').click(function() {
var $row = $(this).closest('tr');
tbid = $row.attr('id'); // table row ID
// .......
});
$('#modelformbuttonclick').click(function() {
console.log("My clicked row was: " + tbid);
});
答案 1 :(得分:0)
您可以使用模式的'data'属性来存储当前的tbid
。
$('tr #modelbox').click(function() {
var $row = $(this).closest('tr');
var tbid = $row.attr('id'); // table row ID
var fieldOption = []
// Save to modal Data
$('#myModal').data('current', tbid);
$row.find('#words option').each(function() { fieldOption.push($(this).val()); });
// console.log(fieldOption);
$('.modal-body-inner').html('');
for(var i = 0, size = fieldOption.length; i < size ; i++){
var item = fieldOption[i];
$('.modal-body-inner').append("<span class=" + "span1" + " id = "+ tbid +"> "+ item + "</span>");
}
});
$('#modelformbuttonclick').click(function() {
// Read from modal Data
console.log( $('#myModal').data('current') );
// console.log($(this).closest('tr').data('id'));
// console.log($(event.target).closest('tr').data('id'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"/>
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css"/>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Choose words. You can add words, delete words </h4>
</div>
<div class="modal-body">
<!-- <input type="text" value="Amsterdam,Washington,Sydney,Beijing,Cairo" data-role="tagsinput" /> -->
<div class="modal-body-inner"></div>
</div>
<div class="modal-footer">
<button type="button" id = "modelformbuttonclick" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<table>
<tr id="1">
<td><td>
<td><span id="modelbox" data-target="#myModal" href="#myModal" data-toggle="modal" class="glyphicon glyphicon-plus"></span></td>
</td>
<tr id="2">
<td><td>
<td><span id="modelbox" data-target="#myModal" href="#myModal" data-toggle="modal" class="glyphicon glyphicon-plus"></span></td>
</td>
<tr id="3">
<td><td>
<td><span id="modelbox" data-target="#myModal" href="#myModal" data-toggle="modal" class="glyphicon glyphicon-plus"></span></td>
</td>
</table>