2014-09-30 89 views
0

我需要获取href的id,并将其包含在javascript中,但我无法正确理解它。 我想要做的是在delete_page.php获取所选择的ID,所以我可以更新记录使用它,并为此工作,我需要在javascript中delete_page.php显示delete_page.php ?id =(),多数民众赞成我需要做的,如果有人有一个更简单的方法来做到这一点将是伟大的!从href中获取id并将其包含在jquery中

这是我的代码

$(document).ready(function() 
{ 
    $('a.delete').click(function() 
    { 
     if (confirm("Are you sure you want to delete this row?")) 
     { 
      var id = $(this).parent().parent().attr('id'); 
      var data = 'id=' + id ; 
      var parent = $(this).parent().parent(); 

      $.ajax(
      { 
        type: "POST", 
        url: "delete_page.php?id=", 
        data: data, 
        cache: false, 

        success: function() 
        { 
         parent.fadeOut('slow', function() {$(this).remove();}); 
        } 
      });     
     } 
    }); 


}); 

<a href="#" class="delete" id="post-<?php echo $pid; ?>" name="delete_id">Erase page</a> 

我需要从脚本里面的HREF该ID的delete_page.php后?ID =”在这里,任何答案将是十分感谢

我也tryed 删除

$(document).ready(function() { 
$('a.delete').click(function (e) { 
    e.preventDefault(); 
    if (confirm("Are you sure you want to delete this row?")) { 
     var id = $(this).attr('id').replace(/post-/, ''), 
      parent = $(this).parent(); 

     $.post("delete_page.php", { 
      "id": id 
      }) 
      .done(function (data) { 
      parent.fadeOut('slow', function() { 
       $(this).remove(); 
      }); 
     }); 
    } 
}); 
}); 

但消息后,脚本的其余部分是不工作时,delete_page.php和褪色.. 继承人的小提琴 http://jsfiddle.net/dqn61zgv/

我想要做的是在delete_page.php获取该选择的ID,所以我可以更新记录使用它,并为此工作我需要在JavaScript中的delete_page.php是以显示delete_page.php?id =(),那就是我需要做的,如果有人有一个更简单的方法来做到这一点将是伟大的!

+0

您可能必须阻止锚点的默认操作...也共享相关的html – 2014-09-30 03:46:52

+2

没有必要在'url:“delete_page.php?id =”'so'url中有'id =' :“delete_page.php”然后是'data:{id:id}' – 2014-09-30 03:47:38

+3

什么是父 - >父?我们可以看到你的标记吗? – 2014-09-30 03:47:40

回答

0

如果您在点击a标签ID ATTR那么你不需要使用

var id = $(this).parent().parent().attr('id'); 

得到它,只需要用

var id = $(this).attr('id'); 

我会去解决这个问题,这样

HTML

<a class="delete" href="#" id="post-8">Delete</a> 

JS

$(document).ready(function() { 
    $('a.delete').click(function (e) { 
     e.preventDefault(); 
     if (confirm("Are you sure you want to delete this row?")) { 
      var id = $(this).attr('id').replace(/post-/, ''), 
       parent = $(this); 

      $.post("delete_page.php", {"id": id }) 
      .done(function (data) { //am using done callback, you can try always to see script works and fade the a tag 
       parent.fadeOut('slow', function() { parent.remove(); }); 
      }); 
     } 
    }); 
}); 

PHP

if(isset($_POST['id']) && !empty($_POST['id'])) { 
    $id = (int) $_POST['id']; 

    //Update your db table 
    ..... 

    //echo result after update 
    echo "success"; 
} 

,这里是working fiddle

确保你的PHP脚本delete_page.php存在和返回过程中经过200 OK响应

我已经更新了代码,请现在它的工作

+0

谢谢你的回复,但这不起作用,当你点击你得到的消息,但其余的一切都不工作,一旦你接受了JavaScript的消息,脚本的其余部分不会工作,PHP文件和淡入淡出不工作 – 2014-09-30 13:06:16

+0

如果你可以分享你的HTML我可以让你的工作,小提琴将无法工作,因为没有找到'delete_page.php'的页面 – Saqueib 2014-09-30 13:08:23

+0

现在检查我已经改变了'parent = $ (this).parent()'part to'parent = $(this);' – Saqueib 2014-09-30 13:12:21

0

a.delete指的是具有类.delete锚标记<a>的所有实例。

看你click();方法:

$('a.delete').click(function(){..

你应该使用$(this).attr("id"),而不是$(this).parent().parent().attr("id");,因为点击方法已经被绑定到上述选择。除非您的HTML另有声明,否则请使用

。请告诉我们。

+0

我刚刚编辑了代码,请检查它 – 2014-09-30 13:09:32