[英]Passing link with parameters with Jquery
I have this link: 我有这个链接:
<a id='delete' href='/add/delete.php?iid=XXXXX'>#</a>
And I want to pass it on through Jquery so the page doesn't refresh. 而且我想通过Jquery传递它,以便页面不会刷新。 However, I'm having a difficult time making that happen.
但是,我很难做到这一点。 All of the click events and ajax I've tried with Jquery have been unsuccessful.
我使用Jquery尝试过的所有单击事件和Ajax均未成功。
Basically, I want the ajax to send delete.php the value in the iid parameter. 基本上,我希望ajax将delete.php的值发送给iid参数。
$(function(){
$("#delete").click(function(e){
e.preventDefault();
$.post("../add/delete.php", { iid: "xxx" } ,function(data){
//do whatever with the result which is in the variable data.
alert(data)
});
});
});
Since it is a delete, use POST. 由于是删除操作,因此请使用POST。 Otherwise some crawlers will crawl your delete page and you will see your tables are empty one fine morning.
否则,一些爬网程序将爬网您的删除页面,并且您会在一个美好的早晨看到表为空。
EDIT : You can associate the value to be passed with the anchor tag in someway. 编辑 :您可以以某种方式将要传递的值与锚标记关联。 Ex :Change your id tag to be in this format del-YouIdTobe deleted ( Ex : del-1,del-2 etc..)
例如:将您的ID标签更改为采用del-YouIdTo格式(例如:del-1,del-2等)。
<a id='delete' href='#' id="del-1>Delte 1 </a>
<a id='delete' href='#' id="del-2>Delte 2 </a>
<a id='delete' href='#' id="del-3>Delte 3 </a>
Script 脚本
$(function(){
$("#delete").click(function(e){
e.preventDefault();
var val=$(this).attr("id").split("-")[0];
$.post("../add/delete.php", { iid: val } ,function(data){
//do whatever with the result which is in the variable data.
alert(data)
});
});
});
Here try this the href='1'
is the id you want to delete, also to replace the text it finds the id="d1"
: 在这里试试这个
href='1'
是您要删除的ID,也替换它找到id="d1"
的文本:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
$(".delete").click(function(e){
var del_id = $(this).attr('href');
e.preventDefault();
$.post("/add/delete.php", { iid: del_id } ,function(data){
//change the link to deleted
$('#d'+del_id).replaceWith('<a href="#">Deleted</a>');
});
});
});
</script>
</head>
<body>
<a class='delete' id="d1" href='1'>Delete</a>
<a class='delete' id="d2" href='2'>Delete</a>
<a class='delete' id="d3" href='3'>Delete</a>
<a class='delete' id="d4" href='4'>Delete</a>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.