JQuery 从 ajax 加载发送元素 id 数据



>我使用此代码加载页面内容而不刷新

<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>

然后我尝试获取 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 代码

<script>
    $(document).on("click","a",function(e){
        $.ajax({
            url: "post_category.php",
            type: "POST",
            data: $(this).attr("id")
        });
 });
</script>

另一页中的 PHP 代码

<?php
if(isset($_POST['id'])){
 echo 'Worked';
} else {
  echo 'No data';
}

jquery 成功获取了 ID,但它没有将数据正确发送到所需的页面,因此它始终未设置,并且在 else 条件下我没有得到任何数据。我尝试了一切,但没有任何效果。请帮忙。

需要以键值对的形式发送数据,以便您可以在其他页面中按键访问它们。这也是对文档的引用,因此 $(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>

问题似乎出在您尝试发布的数据中。您需要发布有效的 JSON 对象。

确保构造一个有效的 javascript 对象,然后使用 JSON.stringify 函数将 javascript 对象转换为 JSON 对象。

使用 $.ajax 调用之外的 $(this) 设置 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>
 $(document).on("click","a",function(e){   
     $.ajax({
        url: "post_category.php",
        type: "POST",
        data: {id:$(this).attr("id")}
      });
  });

试试这个你错过了id:(this).attr("id")

最新更新