[英]JQuery send element id data from ajax loaded
I'm loading the page content without refresh using this code 我正在使用此代码加载页面内容而不刷新
<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>
Then I'm trying to get the id value and send it to another page using ajax, since there's no refresh. 然后,我尝试获取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>
JQuery code jQuery代码
<script>
$(document).on("click","a",function(e){
$.ajax({
url: "post_category.php",
type: "POST",
data: $(this).attr("id")
});
});
</script>
php code in the other page 另一页中的php代码
<?php
if(isset($_POST['id'])){
echo 'Worked';
} else {
echo 'No data';
}
the jquery take the id successfully, but it didn't send the data to the required page correctly so it always unset and I get No data in the else condition. jQuery成功获取了ID,但未将数据正确发送到所需的页面,因此它始终未设置,在else条件下我没有得到任何数据。 I tried everything, but nothing worked. 我尝试了一切,但没有任何效果。 Please help. 请帮忙。
The problem looks to be in the data you are trying to post. 问题似乎出在您要发布的数据中。 You need to post a valid JSON object. 您需要发布有效的JSON对象。
Make sure you construct a valid javascript object and then use the JSON.stringify
function to convert your javascript object into a JSON object. 确保构造一个有效的javascript对象,然后使用JSON.stringify
函数将您的javascript对象转换为JSON对象。
Set your id using $(this)
outside of your $.ajax
call to reference the anchor element instead of the $.ajax
object. 在$.ajax
调用之外使用$(this)
设置ID以引用锚元素而不是$.ajax
对象。
Something like the following should give you the desired result: 如下所示应会为您提供所需的结果:
<script>
$('a').on("click", function (e) {
var id = $(this).attr("id");
$.ajax({
url: "post_category.php",
type: "POST",
data: JSON.stringify({
id: id
})
});
});
</script>
Need to send data in key-value pair, so that you can access them by key in the other page. 需要以键值对形式发送数据,以便您可以在另一页中通过键访问它们。 Also this is referring to the document and thus $(this).attr('id') is null. 同样,这是指文档,因此$(this).attr('id')为null。 You can try the below code as shown: 您可以尝试如下所示的代码:
<script>
$(document).on("click","a",function(e){
var element =e.target;
$.ajax({
url: "post_category.php",
type: "POST",
data: {'id':$(element).attr("id")}
});
</script>
$(document).on("click","a",function(e){
$.ajax({
url: "post_category.php",
type: "POST",
data: {id:$(this).attr("id")}
});
});
Try this you were missing id:(this).attr("id") 试试这个,你错过了id:(this).attr(“ id”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.