weixin_33692284 2019-12-01 16:01 采纳率: 0%
浏览 62

替换内容Jquery ajax

Im using .append on each of the data i want to load but i want to change the data in my site for example if i click button1 it will show the name and price of the room, then if i click button2 now it should show the data of the button2 and remove the data from button1 in my site. thats what i want to happen. Right now it only stacks the data of each button when i press that diff buttons Like Single Room 45$ Family Room 49$ it stacks up.. this is my code

$(document).ready(function() {
        $(".bkng").on("click", function(event) {
              event.preventDefault();

            var id = $(this).data('room-id');
                console.log(id);


                if(id != '')

                {
                    $.ajax(
                {
                    type:"POST",
                    url : "Pages/ajax",
                    data:{id:id},


                    success:function(data)
                    {
                        console.dir(data);
                        if (data) {


                        result = JSON.parse(data);

                        $("#test1").append(result[0]['name']);
                        $("#test2").append(result[0]['price']);
                        $("#test3").append(result[0]['price']);
                        }
                        else {
                             $('#test1').append('no records found');
                        }


                    }
                });
            }


            });
        }); 

Here's the image https://i.stack.imgur.com/0Vh8x.jpg Here is my html code

<div id = "test1" class = "inliners1"></div>
<div class = "inliners2" id = "test3">PHP &nbsp;</div>

        <h3 class = "your-stay" id = "test2">Total:

The button code

 <div class="col-4">
      <h3 class = "bookingroom">PHP 2,500</h3>
      <h5 class = "ab">PER NIGHT</h5>
      <h5 class = "taxes">Including Taxes & Fees</h5>
      <button class="bkng bkn-room trans_200" data-room-id ="1">BOOK NOW</button>
<div class="col-4">
      <h3 class = "bookingroom">PHP 2,750</h3>
      <h5 class = "ab">PER NIGHT</h5>
      <h5 class = "taxes">Including Taxes & Fees</h5>

      <button class="bkng bkn-room trans_200" data-room-id="2">BOOK NOW</button>
  • 写回答

1条回答 默认 最新

  • weixin_33705053 2019-12-01 17:39
    关注

    The problem with append() is that it appends a string to the selector and ads on and on each time you click. You could change your html structure slightly slightly (adding a element) and use html() to show the correct data without overwriting the caption:

    html:

    <div id = "test1" class = "inliners1"><span></span></div>
    <div class = "inliners2" id = "test3">PHP &nbsp;<span></span></div>
    <h3 class = "your-stay" id = "test2">Total:<span></span></h3>
    

    jQuery:

    $("#test1>span").html(result[0]['name']);
    $("#test2>span").html(result[0]['price']);
    $("#test3>span").html(result[0]['price']);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表