javascript - 在 PHP Echo 中调用 Javascript 函数

标签 javascript php jquery ajax

引用This link ,我正在尝试从表中动态删除行。这是我的 Javascript 函数:

function deleteBox(id){

alert ("Inside Method");
if (confirm("Are you sure you want to delete this record?"))
{
  var dataString = 'id='+ id;
  $("#flash_"+id).show();
  $("#flash_"+id).fadeIn(400).html('<img src="img/loading.gif" /> ');
  $.ajax({
  type: "POST",
  url: "delete.php",
  data: dataString,
  cache: false,
  success: function(result){
           if(result){
                $("#flash_"+id).hide();
                // if data delete successfully
                if(result=='success'){
                     //Check random no, for animated type of effect
                     var randNum=Math.floor((Math.random()*100)+1);
                     if(randNum % 2==0){
                        // Delete with slide up effect
                        $("#list_"+id).slideUp(1000);
                     }else{
                        // Just hide data
                        $("#list_"+id).hide(500);
                     }

                }else{
                     var errorMessage=result.substring(position+2);
                     alert(errorMessage);
                }
          }
  }
  });
}
}

但是,在 Php 中从 Echo 调用它似乎并没有调用它。这是我的 PHP 代码:

echo "<td align=\"center\">" . $id."</td>";
echo "<td><a href = 'javascript:deleteBox($id)'>Delete</a></td>";

不对的地方请指正。非常感谢尽早提供帮助。

最佳答案

<td><a href = 'javascript:deleteBox($id)'>Delete</a></td>

echo "<td><a onClick='deleteBox(" . $id . ");'>Delete</a></td>"; 

在我看来,这就是我会做的......

编辑并缩短了jscript;

function deleteBox(idDelete){

alert ("Inside Method");
if (confirm("Are you sure you want to delete this record?"))
{
  $("#flash_" + idDelete).show();
  $("#flash_" + idDelete).fadeIn(400).html('<img src="img/loading.gif" /> ');

  $.post('delete.php', {'id': idDelete}, function(result) {
            if(result){
                $("#flash_" + idDelete).hide();
                // if data delete successfully
                if(result=='success'){
                     //Check random no, for animated type of effect
                     var randNum=Math.floor((Math.random()*100)+1);
                     if(randNum % 2==0){
                        // Delete with slide up effect
                        $("#list_" + idDelete).slideUp(1000);
                     }else{
                        // Just hide data
                        $("#list_" + idDelete).hide(500);
                     }

                }else{
                     var errorMessage=result.substring(position+2);
                     alert(errorMessage);
                }
          }
  });  
} 

在你的 delete.php 中:

$_POST['id'] 

检索 ID。

关于javascript - 在 PHP Echo 中调用 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24968775/

相关文章:

javascript 日期返回未定义

javascript - 检测 iOS-WebApp 并更改 CSS 属性

javascript - 让用户在网页上创建更多元素

javascript - jQuery .submit() 不适用于 Gravity Forms(Wordpress 插件)

javascript - 通过javascript或jquery减少php中的图像文件大小

javascript - jQuery 切换按钮不透明度

javascript - 删除表单数组元素

php - 如何防止写入已启动的 PHP session ?

php - mysql_connect : Access denied

javascript - JotForm 与 JQuery 冲突