jQuery将数据属性发布到下一页 [英] jquery posting data attribute to the next page
问题描述
我正在尝试将存储在数据属性中的数据发布到下一页,这是我的代码
I am trying to post data stored in a data-attr to the next page, here is my code
首先,我向所有锚标记添加一个类和数据属性,如下所示:
First i add a class and data-attr to all anchor tags like this:
jQuery("a").addClass("wptl_link");
jQuery('a').attr('data-wptl_ip', '<?php echo $_SERVER['REMOTE_ADDR']; ?>');
上传时我已经检查了我的源代码,一切正常.
I have checked my source code when uploaded and this works fine.
我想为每个锚标记添加功能,以将该锚的数据属性提交到下一页.
I would like to add functionality to each anchor tag, to submit the data-attr of that anchor to the next page.
我现在不确定该怎么做.我已经读过.ajax和.post了,不知道有什么区别?
I am now not quite sure on what to do. I have read into .ajax and .post and not sure what the difference is?
我需要做以下事情吗?
jQuery.ajax({
type: "POST",
url: nextpage.php,
data: { wptl_ip: "<?php echo $_SERVER['REMOTE_ADDR']; ?>"},
success: success,
dataType: dataType
});
推荐答案
如果我正确阅读了此说明,则当您单击定位标记时,您试图在页面之间携带一些变量.
If I am reading this correctly, you are trying to carry some variable from page to page, when you click an anchor tag.
$.ajax
将请求发送到另一个页面,而无需移动到该页面. $.ajax
用于从其他地方异步更新或获取数据.无需刷新页面.
$.ajax
sends the request to another page without moving to the page. $.ajax
is used a lot for updating or getting data asynchronously, from other places. Without having to refresh the page.
您可以将数据属性存储在 sessionStorage
变量中.
You can store the data-attr in a sessionStorage
variable.
$('body').on('click','a',function(e){
e.preventDefault();
var location = $(this).attr('href');
var data = $(this).attr('data-wptl_ip');
sessionStorage.setItem('wptl_ip', data);
location.href= location;
});
然后您可以使用
var myData = sessionStorage.getItem('wptl_ip');
这是假设您要与数据一起移至下一页.
This is assuming you want to move to the next page, along with the data.
发布到php文件以插入数据库中
Posting to a php file to insert in Database:
$('body').on('click','a',function(e){
e.preventDefault();
var location = $(this).attr('href');
var data = $(this).attr('data-wptl_ip');
$.ajax({
url: 'somePage.php',
data: {someData: data}
}).done(function(response){
sessionStorage.setItem('wptl_ip', data);
location.href= location;
});
});
这篇关于jQuery将数据属性发布到下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!