我在php环境中使用以下代码:
<tr class="odd gradeX">
<td><?php echo $blog_id; ?></td>
<td><?php echo $blog_title; ?></td>
<td><?php echo $cat_name; ?></td>
<td class="center"><img width="50" src="<?php echo SITE_URL."assets/images/blog_images/$blog_image" ?>"/></td>
<td class="center"><?php echo $uname; ?></td>
<td class="center"><?php echo $added_date; ?></td>
<td class="center"><?php echo $status; ?></td>
<td class="center"><a data-toggle="modal" class="delete" data-id="<?php echo $blog_id; ?>" data-target="#myModal" href="<?php echo AD_SITE_URL."delete.php?name=blog&blog_id=$blog_id" ?>">Delete</td>
</tr>
当我点击delete
链接时,它会显示一个带有以下代码的弹出框:
<form >
<div class="modal-footer">
<input type="submit" id="deletePost" class="btn btn-danger" name="submit" value="YES">
<input type="submit" class="btn btn-success" name="submit" value="NO" data-dismiss="modal">
</div>
</form>
现在当我按下Yes
按钮时,它应该显示data-id
链接中的delete
值,但每次它都显示我data-id
delete
1}}价值。如何使用jQuery获取每个data-id
链接<script>
$(document).ready(function() {
$("#deletePost").click(function() {
var id = $(".delete").attr("data-id");
alert(id);
});
});
</script>
值?
我使用以下代码:
list_possible_states
答案 0 :(得分:4)
您只需在模式中创建一个隐藏字段,并在每次用户点击删除链接时填充其值
这是工作演示
$(function() {
$(".delete").click(function() {
id = $(this).data('id');
$("#myModal #post-id").val(id);
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet" />
<table class="table">
<tr>
<th>Id</th>
<th>Title</th>
<th>Category</th>
<th>Desc</th>
<th></th>
</tr>
<tr class="odd gradeX">
<td>1</td>
<td>title 1</td>
<td>Category</td>
<td class="center">data</td>
<td class="center"><a data-toggle="modal" class="delete" data-id="1" data-target="#myModal" href="#">Delete</a>
</td>
</tr>
<tr class="odd gradeX">
<td>2</td>
<td>title 2</td>
<td>Category</td>
<td class="center">data</td>
<td class="center"><a data-toggle="modal" class="delete" data-id="2" data-target="#myModal" href="#">Delete</a>
</td>
</tr>
<tr class="odd gradeX">
<td>3</td>
<td>title 3</td>
<td>Category</td>
<td class="center">data</td>
<td class="center"><a data-toggle="modal" class="delete" data-id="3" data-target="#myModal" href="#">Delete</a>
</td>
</tr>
<tr class="odd gradeX">
<td>4</td>
<td>title 4</td>
<td>Category</td>
<td class="center">data</td>
<td class="center"><a data-toggle="modal" class="delete" data-id="4" data-target="#myModal" href="#">Delete</a>
</td>
</tr>
<table>
<div id="myModal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>Dou you want to delete</p>
this is your hidden field for id
<input type="text" name="id" id="post-id">
</div>
<div class="modal-footer">
<input type="submit" id="deletePost" class="btn btn-danger" name="submit" value="YES">
<input type="submit" class="btn btn-success" name="submit" value="NO" data-dismiss="modal">
</div>
</form>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
&#13;
我希望它会有所帮助
答案 1 :(得分:1)
var id;
$("#tableid tbody").on('click','.delete', function(e){
// show your modal
id = $(this).attr("data-id");
});
答案 2 :(得分:0)
每次单击此行的删除按钮,然后设置其他data-id属性以提交按钮
$('.delete').on('click',function(){
$('[name=submit]').attr('data-id',$(this).data('id'));
})
&安培;关闭数据模型,你可以删除该属性,(你可以在ajax成功点击提交时重复删除属性功能)
$('.close').on('click', function(){
$('[name=submit]').removeAttr('data-id'))
})