duanmianxue2687 2018-02-09 18:54
浏览 40
已采纳

显示多个Ajax html响应

The $('.my-button').click(function(e) function is supposed to show the output of the display.php, which just contains some mysql query and the output in html. It works so far, but as I am looping the button itself for each post, the function only works for the first button. I need to make the script understand, that for each button it should only show the results for that button. But how to insert some individual ID to the $(".responsecontainer").html(response);? I tried with $(".responsecontainer" + id).html(response); getting "id" before via parameters, but it seems that this id must be hardcoded.

Here is the full code:

$(document).ready(function() {
$('.my-button').click(function(e) {                
  $.ajax({    //create an ajax request to display.php
    type: "GET",
    url: "display.php",             
    dataType: "html",   //expect html to be returned                
    success: function(response){                    
        $(".responsecontainer").html(response); 
        //alert(response);
    }

And this is the button and DIV to show results:

<input type="button" class="my-button" value="'.$plus.'" />
<div class="responsecontainer" id="'.$plus.'" align="center">
</div>      

Noting that $plus is always unique.

  • 写回答

2条回答 默认 最新

  • duanjiao8871 2018-02-09 19:05
    关注
    • Use data-attributes to store the related DIV's id.
    • When a button is clicked get the data-attribute, i.e: data-target-id.
    • With that id you can execute a selector as follow: #div_1 and insert the received HTML.
    • DON'T use value to target your DIV, the button's value is the text for your buttons, just imagine if your button texts are filled using a kind of i18n translation.

    $(document).ready(function() {
      $('.my-button').click(function(e) {
          var targetId = $(this).data('target-id');
          /*$.ajax({ //create an ajax request to display.php
              type: "GET",
              url: "display.php",
              dataType: "html", //expect html to be returned                
              success: function(response) {*/
                $(`#${targetId}`).html(`Response = ${targetId}`);
              /*});
          });*/
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="button" data-target-id='div_1' class="my-button" value="Click me!" />
    <div id='div_1' class="responsecontainer" align="center">
    </div>
    
    <input type="button" data-target-id='div_2' class="my-button" value="Click me!" />
    <div id='div_2' class="responsecontainer" align="center">
    </div>
    
    <input type="button" data-target-id='div_3' class="my-button" value="Click me!" />
    <div id='div_3' class="responsecontainer" align="center">
    </div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路