Javascript 如何在jquery中追加返回值

Javascript 如何在jquery中追加返回值,javascript,php,jquery,html,Javascript,Php,Jquery,Html,在上一页返回后,我从数据库中选择了匹配的记录,在这里我得到了所有值,但我不知道如何将这些返回值附加到我需要的页面中,而不是房间2,房间3…在值中追加值。房间号和此处的PG名称追加值。bg\U id 我有这样的价值观 var res =jQuery.parseJSON(data); console.log(res); 计数:2数据:数组[2]0:对象1:对象 预订状态:“1” id:“2” pg_id:“1” 租金:“4000” 房间号:“2号房间” 房间共享:“2” 预订

在上一页返回后,我从数据库中选择了匹配的记录,在这里我得到了所有值,但我不知道如何将这些返回值附加到我需要的页面中,而不是房间2,房间3…值中追加值。房间号和此处的PG名称追加值。bg\U id


我有这样的价值观

var res =jQuery.parseJSON(data);
         console.log(res);
计数:2数据:数组[2]0:对象1:对象

预订状态:“1”

id:“2”

pg_id:“1”

租金:“4000”

房间号:“2号房间”

房间共享:“2”


预订状态:“1”

id:“3”

pg_id:“1”

租金:“4000”

房间号:“3号房间”

房间共享:“2”

index.php
函数showDiv(切换){
var sharing=$(“#sharing”).val();
$.ajax({
类型:“POST”,
url:“pg_details.php”,
数据:“共享_id=“+共享,
成功:功能(数据){
var res=jQuery.parseJSON(数据);
控制台日志(res);
如果(res['return']==1){
var htmlString='';
$.each(res['data'],函数(键,值){
htmlString=''+value.pg_id+'高级设备V.wifibed洗衣机';
});
$(“.view_room”).prepend(htmlString);
}
}
});
}
pg_details.php
请尝试以下操作:

$.ajax({
  type: "POST",
  url: "pg_details.php",
  data: "sharing_id=" + sharing,
  success: function(data) {
    var res = jQuery.parseJSON(data);

    $.each(res.data, function(key, value) {
     el = $('[data-id='+value.pg_id +']');
     if (el.length) {
         el.find('.btnmar').append(' <a href="register.php?id='+value.id +'">
            <button type="button" class="btn btn-primary" style=" width: 71px; ">'+value.room_number+'</button>
          </a>&nbsp;&nbsp;');
      } else {
     var htmlString =  '<div id="toggle" data-id="'+value.pg_id +'">
 <div class="container" style=" margin-bottom: 30px;">
    <div class="row">
      <h4 style="margin-left:15px;">'+value.pg_id +'</h4>
      <div class="col-sm-10">
        <div class="btn-group btnmar">
          <a href="register.php?id='+value.id +'">
            <button type="button" class="btn btn-primary" style=" width: 71px; ">'+value.room_number+'</button>
          </a>&nbsp;&nbsp;
        </div>
      </div>
      <div class="col-sm-2">
        <div class="panel-group">
          <div class="panel panel-primary">
            <div class="panel-heading"> Premium Facility</div>
            <div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-television" aria-hidden="true" style="margin-right:15px;"></i>T.V.</div>
            <div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-wifi" aria-hidden="true" style="margin-right:15px;"></i>Wifi</div>
            <div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-bed" aria-hidden="true" style="margin-right:15px;"></i>Bed</div>
            <div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-shopping-basket" aria-hidden="true" style="margin-right:15px;"></i>Washing Machine</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>';
   $(".view_room").prepend(htmlString);}
    });


  }
});
或者使用联接查询返回名称和数据

尝试以下操作:

$.ajax({
  type: "POST",
  url: "pg_details.php",
  data: "sharing_id=" + sharing,
  success: function(data) {
    var res = jQuery.parseJSON(data);

    $.each(res.data, function(key, value) {
     el = $('[data-id='+value.pg_id +']');
     if (el.length) {
         el.find('.btnmar').append(' <a href="register.php?id='+value.id +'">
            <button type="button" class="btn btn-primary" style=" width: 71px; ">'+value.room_number+'</button>
          </a>&nbsp;&nbsp;');
      } else {
     var htmlString =  '<div id="toggle" data-id="'+value.pg_id +'">
 <div class="container" style=" margin-bottom: 30px;">
    <div class="row">
      <h4 style="margin-left:15px;">'+value.pg_id +'</h4>
      <div class="col-sm-10">
        <div class="btn-group btnmar">
          <a href="register.php?id='+value.id +'">
            <button type="button" class="btn btn-primary" style=" width: 71px; ">'+value.room_number+'</button>
          </a>&nbsp;&nbsp;
        </div>
      </div>
      <div class="col-sm-2">
        <div class="panel-group">
          <div class="panel panel-primary">
            <div class="panel-heading"> Premium Facility</div>
            <div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-television" aria-hidden="true" style="margin-right:15px;"></i>T.V.</div>
            <div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-wifi" aria-hidden="true" style="margin-right:15px;"></i>Wifi</div>
            <div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-bed" aria-hidden="true" style="margin-right:15px;"></i>Bed</div>
            <div class="panel-body" style=" padding-top: 5px; padding-bottom: 5px;"><i class="fa fa-shopping-basket" aria-hidden="true" style="margin-right:15px;"></i>Washing Machine</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>';
   $(".view_room").prepend(htmlString);}
    });


  }
});

或者使用连接查询返回名称和数据

如果(res['return']==1){
,则删除此条件。。这并不意味着存在,并将附加代码弯到工作状态。另外@poonam在
each()
中完全正确。使用
htmlString+='
而不是
htmlString='
,在循环后使用
$(.view\u-room”)。附加(htmlString);
您可以使用
if(res)
而不是
if(res['return']==1)
My DB like this pg_id=1。两个共享房间2房间的可用名称,如Room2和Room3,此处的值返回正确,但在显示Room2和fecilities时,显示一行和Room3,fecilities为我显示另一行,我不希望这样,我需要像Room2和Room3一样的房间3 fecilities是同一行,怎么办?假设pg_idis Different意味着我想显示第一行,如pg_id、房间号、生育能力和第二行pg_id等。如果(res['return']==1){,则删除此条件。{code>。这并不意味着存在并弯腰让附加代码工作。此外,@poonam在
中完全正确
使用
htmlString+='
而不是
htmlString='
并在循环后使用
$(“.view_room”)。追加(htmlString);
您可以使用
if(res)
而不是
if(res['return']==1)
My DB like this pg_id=1。两个共享房间2房间的可用名称,如Room2和Room3,此处的值返回正确,但在显示Room2和fecilities时,显示一行和Room3,fecilities为我显示另一行,我不希望这样,我需要像Room2和Room3一样的房间3 fecilities是同一行,怎么办?假设pg_idis Different意味着,当我想显示第一行,如pg_id和房间号、生育能力和第二行pg_id等时,我的DB,如pg_id=1。两个共享房间2的房间可用名称,如Room2和Room3,这里的值返回正确,但在显示房间2和生育能力时,显示一行和房间3,生育能力显示另一行我不想要这样的行我需要像2号房间3号房间的生育力是同一行怎么办?假设pg_id是不同的,这意味着我要显示第一行的pg_id和房间号、生育力和第二行pg_id等…实际上,我不想显示pg_id,而是要显示pg_名称,已经在functions.php中写入,函数名是Getpgname($id)这叫什么function@ManiM您通常如何调用php函数JS如何调用php函数以及如何在该函数中传递参数My DB,如pg_id=1。两个共享房间2房间的可用名称,如Room2和Room3,此处的值返回正确,但在显示Room2和fecilities时,显示一行,Room3和Fecilityies为我显示另一行不想这样我需要像2号房间3号房间的生育能力是同一行怎么办?假设pg_id不同意味着我要显示第一行,如pg_id、房间号、生育能力和第二行pg_id等…实际上,我不想显示pg_id,而是要显示pg_名称,已经在functions.php和function n中编写ame是Getpgname($id);如何调用此calfunction@ManiM通常如何调用php函数如何调用php函数以及如何在该函数中传递参数