Javascript JQuery从加载的ajax发送元素id数据
我正在使用此代码加载页面内容而不刷新Javascript JQuery从加载的ajax发送元素id数据,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我正在使用此代码加载页面内容而不刷新 <script> $(document).ready(function(){ //set trigger and container var trigger = $('#nav ul li a'), container = $('#container'); //do on click trigger.on
<script>
$(document).ready(function(){
//set trigger and container
var trigger = $('#nav ul li a'),
container = $('#container');
//do on click
trigger.on('click', function(e){
/***********/
e.preventDefault();
//get the link location that was clicked
pageurl = $(this).attr('href');
//to change the browser URL to th if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl+'.php');
/* reload content without refresh */
//set loading img
$('#container').append('<div id = "loading">WAIT... <img src = "img/ajax-loader-small.gif" alt="Currently loading" /></div>');
//change img location to center
$("#loading").css({"position": "absolute", "left": "50%", "top": "50%"});
//get the trigger to reload the contents
var $this = $(this),
target = $this.data('target');
container.load(target + '.php');
});
});
// fix url in the browser when click on backward and foreword arrows
$(window).bind('popstate', function() {
$.ajax({url:location.pathname+'?rel=tab',success: function(data){
$('#container').html(data);
}});
});
</script>
$(文档).ready(函数(){
//设置触发器和容器
风险值触发器=$('nav ul li a'),
容器=$(“#容器”);
//点击即做
在('click',函数(e)上触发{
/***********/
e、 预防默认值();
//获取已单击的链接位置
pageurl=$(this.attr('href');
//将浏览器URL更改为th if(pageurl!=window.location){
pushState({path:pageurl}',pageurl+'.php');
/*重新加载内容而不刷新*/
//设置加载img
$(“#容器”).append('WAIT…');
//将img位置更改为中心
$(“#加载”).css({“位置”:“绝对”,“左”:“50%”,“顶部”:“50%”);
//获取触发器以重新加载内容
变量$this=$(this),
target=$this.data('target');
load(target+'.php');
});
});
//单击后退箭头和前进箭头时修复浏览器中的url
$(窗口).bind('popstate',function(){
$.ajax({url:location.pathname+'?rel=tab',success:function(data){
$('#container').html(数据);
}});
});
然后我尝试获取id值并使用ajax将其发送到另一个页面,因为没有刷新
<li><a href="#"><strong>CATEGORY</strong><span> MENU ELEMENTS</span></a>
<ul class="sub-menu">
<li><a id="id_1" data-target='post_category'>Sub_1</a></li>
<li><a id="id_2" data-target='post_category'>Sub_2</a></li>
<li><a id="id_3" data-target='post_category'>Sub_3</a></li>
<li><a id="id_4" data-target='post_category'>Sub_4</a></li>
<li><a id="id_5" data-target='post_category'>Sub_5</a></li>
<li><a id="id_6" data-target='post_category'>Sub_6</a></li>
<li><a id="id_7" data-target='post_category'>Sub_7</a></li>
</ul>
</li>
- 附属法例1
- 次级方案2
- 次级方案3
- 第4分节
- Sub_5
- Sub_6
- Sub_7
JQuery代码
<script>
$(document).on("click","a",function(e){
$.ajax({
url: "post_category.php",
type: "POST",
data: $(this).attr("id")
});
});
</script>
$(文档)。在(“单击”,“a”,函数(e){
$.ajax({
url:“post_category.php”,
类型:“POST”,
数据:$(this.attr(“id”)
});
});
另一页中的php代码
<?php
if(isset($_POST['id'])){
echo 'Worked';
} else {
echo 'No data';
}
问题似乎出在您试图发布的数据中。
您需要发布一个有效的JSON对象
确保构造了有效的javascript对象,然后使用JSON.stringify
函数将javascript对象转换为JSON对象
使用$(this)
在$.ajax
调用之外设置id,以引用锚元素而不是$.ajax
对象
类似于以下内容的内容将为您提供所需的结果:
<script>
$('a').on("click", function (e) {
var id = $(this).attr("id");
$.ajax({
url: "post_category.php",
type: "POST",
data: JSON.stringify({
id: id
})
});
});
</script>
$('a')。在(“单击”)上,函数(e){
var id=$(this.attr(“id”);
$.ajax({
url:“post_category.php”,
类型:“POST”,
数据:JSON.stringify({
id:id
})
});
});
需要发送键值对中的数据,以便您可以在另一页中通过键访问它们。
这也是指文档,因此$(this.attr('id')为空。
您可以尝试以下代码,如图所示:
<script>
$(document).on("click","a",function(e){
var element =e.target;
$.ajax({
url: "post_category.php",
type: "POST",
data: {'id':$(element).attr("id")}
});
</script>
$(文档)。在(“单击”,“a”,函数(e){
var元素=e.目标;
$.ajax({
url:“post_category.php”,
类型:“POST”,
数据:{'id':$(元素).attr(“id”)}
});
试试这个你缺少的id:(this).attr(“id”)试试这个:data{id:$(this).attr(“id”)}另一个可能是:data:“id=”+$(this.attr(“id”)谢谢,但这两个代码都不起作用。$(this)
不是你想象的那样。它绑定到ajax()
函数,而不是('click')上的
。这是一个典型的词法范围(_that=This)问题。谢谢,但我仍然没有得到任何数据,否则会引发错误no dataSee更新的答案,你的$(This)不是引用锚,而是引用上面代码中没有设置id的文档,$(This)
实际上绑定到ajax()
方法,而不是()上的on
方法(这是您所需要的)。因此id:$(this.attr(“id”)
在您的示例中将是未定义的。谢谢,但我仍然没有得到任何数据,否则条件会触发错误no data尝试在ajax请求中添加一个成功回调。查看请求是否成功完成。@AhmadzIssaI添加了一个成功回调,我得到“工作”从if语句,但在页面中仍然没有数据!成功回调放在哪里了。你能在代码中显示吗?我还建议尝试编辑的代码。$(文档)。在(“单击”,“a”,函数(e){$.ajax({url:“post_category.php”,键入:“post”,数据:{id':$(this.attr(“id”)},成功:函数(数据){alert(data);}}}};
$(document).on("click","a",function(e){
$.ajax({
url: "post_category.php",
type: "POST",
data: {id:$(this).attr("id")}
});
});