我是编程新手,所以希望能在这里得到一些答案
背景:我使用的是PHP,它提取一个对象id,我基本上有一个输入类型按钮,一旦点击它就会给出一个警告来确认你是否想要继续,一旦确认了它就会更新数据库(比如重新认证)。
现在我想用SweetAlert替换这个基本的警告,所以我不得不把"a“元素替换为"input”。
下面是HTML代码
<input type="button" onclick="myFunction();" class="btn btn-info" value="Recertify" />
<input type="hidden" id="recertid" value="<?php echo $id; ?>"/>
下面是JavaScript
<script>
function myFunction(){
swal({
title: "",
text: "Are you sure you want to recertify?",
type: "warning",
showCancelButton: true,
showConfirmButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Recertify!",
cancelButtonText: "I am not sure!",
closeOnConfirm: false,
closeOnCancel: false,
},
function(isConfirm){
if (isConfirm)
{
var sid = document.getElementById("recertid").value;
swal({
title: "",
text: "Schedule recertified!",
type: "success",
timer: 1000
}, function(){
window.location = "recertify.php?id=" + sid;
}
);
} else {
//return false;
swal({
title: "",
text: "No action taken!",
type: "error",
timer: 1000
});
}
});
}
</script>
如您所见,使用window.location所需的id被传递给另一个php。
警报没有问题,工作得很好。问题是只有表行中的第一个$id被传递到window.location中。
我们如何根据在行中单击的按钮来传递值?尝试在网上寻找答案,但找不到正确的方法
任何帮助我们都将不胜感激
发布于 2018-06-16 08:36:38
如果我没理解错的话,你有几行有按钮。您需要通过window.location将相应的被单击按钮的行'recertid / id‘传递给您的PHP脚本。
让我们将'id‘值传递给函数"myFunction('$id')“
<input type="button" onclick="myFunction('<?php echo $id; ?>');" class="btn btn-info" value="Recertify" />
<input type="hidden" id="recertid" value="<?php echo $id; ?>"/>
<script>
function myFunction(recertid){
swal({
title: "",
text: "Are you sure you want to recertify?",
type: "warning",
showCancelButton: true,
showConfirmButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Recertify!",
cancelButtonText: "I am not sure!",
closeOnConfirm: false,
closeOnCancel: false,
},
function(isConfirm){
if (isConfirm)
{
swal({
title: "",
text: "Schedule recertified!",
type: "success",
timer: 1000
}, function(){
window.location = "recertify.php?id=" + recertid;
}
);
} else {
//return false;
swal({
title: "",
text: "No action taken!",
type: "error",
timer: 1000
});
}
});
}
</script>
https://stackoverflow.com/questions/50883039
复制相似问题