首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将输入元素值(php)传递给Sweetalert javascript

将输入元素值(php)传递给Sweetalert javascript
EN

Stack Overflow用户
提问于 2018-06-16 05:45:04
回答 1查看 1K关注 0票数 0

我是编程新手,所以希望能在这里得到一些答案

背景:我使用的是PHP,它提取一个对象id,我基本上有一个输入类型按钮,一旦点击它就会给出一个警告来确认你是否想要继续,一旦确认了它就会更新数据库(比如重新认证)。

现在我想用SweetAlert替换这个基本的警告,所以我不得不把"a“元素替换为"input”。

下面是HTML代码

代码语言:javascript
复制
<input type="button" onclick="myFunction();" class="btn btn-info" value="Recertify" />
<input type="hidden" id="recertid" value="<?php echo $id; ?>"/>

下面是JavaScript

代码语言: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中。

我们如何根据在行中单击的按钮来传递值?尝试在网上寻找答案,但找不到正确的方法

任何帮助我们都将不胜感激

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-16 08:36:38

如果我没理解错的话,你有几行有按钮。您需要通过window.location将相应的被单击按钮的行'recertid / id‘传递给您的PHP脚本。

让我们将'id‘值传递给函数"myFunction('$id')“

代码语言:javascript
复制
<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50883039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档