gpt4 book ai didi

javascript - 如何将 data-id 变量从模式传递到 php 页面

转载 作者:行者123 更新时间:2023-12-03 07:12:59 24 4
gpt4 key购买 nike

我有以下 DELETE 按钮,我通过 data-id 传递 $userid

<a href='#myModal' class='trash' data-id='".$userid."' role='button'data-toggle='modal'>
Delete</a>

我有以下模式

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Confirm Delete</h4>
</div>
<div class="modal-body">
<p>You are about to delete <b><i class="title"></i></b> record, this procedure is irreversible.</p>
<p>Do you want to proceed?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<a href="#" class="btn btn-danger" id="modalDelete">Delete</a>
</div>
</div>
</div>
</div>

下面的 JS 获取 DELETE 按钮的值

$('.trash').click(function(){
var id=$(this).data('id');
$('#modalDelete').attr('href','delete_user.php?id=' + id);
});

我试图在模式中设置“href”的值,以便它可以传递到一个名为delete_user.php的php页面,该页面从数据库中删除用户。有人看到我哪里出错了吗?我无法获取转到delete_user.php的href

最佳答案

你这里有错误data-id='".$userid."'应该是data-id='<?php echo $userid;>'

<a href='#myModal' class='trash' data-id='<?php echo $userid;>' role='button'data-toggle='modal'>
Delete</a>

为了更好的方法,去掉 click函数,使用 modal event让 bootstrap 处理剩下的事情

$(document).ready(function() {
$('#myModal').on('show.bs.modal', function(e) {
var id = $(e.relatedTarget).data('id');
alert(id);
$('#modalDelete').attr('href', 'delete_user.php?id=' + id);
});
});

Fiddle

关于javascript - 如何将 data-id 变量从模式传递到 php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36550004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com