javascript - javascripts是否可以有带有ID的hrefs?
问题描述
是否可以在 runConfirm 函数中添加一个包含 id 的 href?我的目标是在我单击接受后出现一个弹出窗口,并且接受按钮从 isset 进行查询。
这是我接受的代码。
$displaybody .= "<tr>
<td>" . $rows['lname'] . ", " . $rows['fname'] . "</td>
<td>" . $rows['subject'] . "</td>
<td>" . $rows['days'] . " " . $rows['rstime'] . " - " . $rows['retime'] . "
</td>
<td>" . $rows['note'] . "</td>
<td>
<a class='runConfirm' data-id='" . $rows['rid'] . "'>Accept</a>
<a href='home.php?decline=" . $rows['rid'] . "'>Decline</a>
</td>
</tr>"
这是我的弹出功能。(更新)
<script type='text/javascript'>
$(function(){
$('.runConfirm').click(function(event){
// get the id you want to act on
var theId = $(this).data('rid');
// ask user for confirmation
alertify.confirm('Are you sure to accept this request?',
// accepted
function(){
// go to the processing page
window.location('/accept.php?rid=' + theId);
},
// declined
function(){
alertify.error('You clicked CANCEL');
}).set('closable', false);
});
});
</script>
Accept.php 包含接受预订的过程。
<?php
include("server.php");
session_start();
if(isset($_GET['accept']))
{
$rid=$_GET['accept'];
$query=$mysqli->query("SELECT qid FROM requests WHERE rid='".$rid."'");
if($query->num_rows>0)
{
while($rows=$query->fetch_assoc())
{
$qid=$rows['qid'];
}
}
$query1=$mysqli->query("UPDATE qualified SET status='accepted' WHERE
qid='".$qid."' ");
$query=$mysqli->query("UPDATE requests SET action='accepted' WHERE
rid='".$rid."'");
header("Location:home.php");
}
?>
解决方案
生成 DOM 时,您可以将数据属性添加到“确认”链接,然后您可以从runConfirm
点击回调函数访问该链接。
假设您要使用的 id$rows['rid']
将给出:(
为清楚起见仅显示更改的部分)
dom 生成
$displaybody .= "<tr>
<td>" . $rows['lname'] . ", " . $rows['fname'] . "</td>
<td>" . $rows['subject'] . "</td>
<td>" . $rows['days'] . " " . $rows['rstime'] . " - " . $rows['retime'] . "</td>
<td>" . $rows['note'] . "</td>
<td>
<a class='runConfirm' data-id='" . $rows['rid'] . "'>Accept</a>
<a href='home.php?decline=" . $rows['rid'] . "'>Decline</a>
</td>
</tr>"
弹出窗口处理(更新)
$('.runConfirm').click(function(event){
// get the id you want to act on
var theId = $(this).data('id');
// ask user for confirmation
alertify.confirm('Are you sure to accept this request?',
// accepted
function(){
// go to the processing page
window.location = '/accept.php?rid=' + theId;
},
// declined
function(){
alertify.error('You clicked CANCEL');
}).set('closable', false);
});
你的第三个片段会进入accept.php
.
您的代码将:
使用行 ID 作为数据属性呈现您的表格
当点击“Accept”时,提示用户确认并将他发送到“accept.php”页面。
在服务器上运行后处理代码,最终将用户重定向到“home.php”
您可以在文档中找到有关 jQuery 数据的更多信息:https ://api.jquery.com/data/
推荐阅读
- javascript - 如何使我的标题移动响应?
- haskell - 在 Haskell 的 do 块中使用 let 中的状态
- reactjs - 具有自定义数据提供者的自定义管理组件
- html - 如何解决 iframe 的填充问题?
- c# - Web API 空白参数值被转换为 null
- eslint - gulp-eslint 未输出到文件 - 无法正确配置 writableStream
- brain.js - 从json导入LSTM网络的brain.js问题
- sql - 使用 MAX 和 GROUP BY 时如何只得到一个结果
- python - 当我尝试为线性回归运行此代码时,输入包含 NaN、无穷大或对于 dtype('float64') 错误而言太大的值
- amazon-web-services - AWS - 如何使用 Javascript SDK 获取 Cloudfront 指标