douchuanhan8384 2015-03-06 17:11
浏览 32
已采纳

如何获得混合php和jQuery的“this”变量?

I have a set of boxes:

HTML

<button class="btn-modal">
   <div>Ajax content 1</div>
</button>
<button class="btn-modal">
   <div>Ajax content 1</div>
</button>

In the PHP loop I do:

<?php 
$permalink = get_permalink(); // Link of the box
?>
<script>
var simple = '<?php echo $permalink; ?>';
</script>

The php loop above prints the correct link for each box link in each simple variable

I then call the content via ajax like this:

$(document).on( 'click', ".btn-modal", function(){
  var cont = $(this).simple +  " .content"; // Load via ajax the Box link + content 
  jQuery(".modal-body").load(cont);
});

The variable simple is applied to all the boxes correctly but I am not getting this box link on click but I am getting a no found error instead

http://www.example.com/xchanges/home/work/interactive/undefined 404 (Not Found)
  • 写回答

1条回答 默认 最新

  • doulvyi2172 2015-03-06 17:36
    关注

    I guess i understand what you want to do. Your approach is not the correct way, you want to save a link for each div and then use that link in some JS logic.

    Then in your loop (this is Wordpress right ?) you need to set out the link in the DOM element you want inside a data-attr and also give some class name to that div so that you can actually select it using JS

    echo '<div class="someClass" data-src='. get_permalink() . '> ..... 
    

    The result should be:

    <div class="someClass" data-src="http://..." >Ajax content 1</div>
    

    Now, your JS

    $(document).on( 'click', ".btn-modal", function(){
      var cont = $(this).children('.someClass').attr('data-src') + " .content";
      jQuery(".modal-body").load(cont);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊