如何在jquery中添加返回值
How to append the return values in jquery
这里我从数据库中选择了匹配的记录,之后我在上一页返回,在这里我得到了所有的值,但我不知道如何在这个页面中添加这个返回值,我需要像而不是房间2,房间3…在value中追加值。room_number and PG NAME HERE append in value.bg_id
我得到了这样的值
var res =jQuery.parseJSON(data);
console.log(res);
count:2 data:Array[2] 0:Object 1:Object
booking_status:"1"
id:"2"
pg_id:"1"
租金:"4000"
room_number:"房间2"
room_sharing:"2"
booking_status:"1"
id:"3"
pg_id:"1"
租金:"4000"
room_number:"房间3"
room_sharing:"2"
index.php
<script>
function showDiv(toggle){
var sharing=$("#sharing").val();
$.ajax({
type: "POST",
url: "pg_details.php",
data: "sharing_id="+sharing,
success: function(data) {
var res =jQuery.parseJSON(data);
console.log(res);
if(res['return'] == 1){
var htmlString='';
$.each( res['data'], function( key, value ){
htmlString ='<div id="toggle"><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" id="btnmar"><a href="register.php?id=2"><button type="button" class="btn btn-primary" style=" width: 71px; ">'+value.room_number+'</button></a> </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);
}
}
});
}
</script>
pg_details.php
<?php
include_once("admin/config.php");
include("functions.php");
//$pg_name=Getpg($_POST['pg_id']);
$sharing=$_POST['sharing_id'];//Getting Sharing Value
$sql=mysql_query("SELECT * FROM rooms WHERE room_sharing='$sharing'");
$count = mysql_num_rows($sql);
if($count > 0){
while($row=mysql_fetch_assoc($sql)){
$data[]= $row;
}
$pg_type= array("return"=>1,"count" =>$count,"data" =>$data);
echo $pg_type = json_encode($pg_type);
}else{
$pg_type= array("return"=>0,"count" =>0,"data" =>"");
echo $pg_type = json_encode($pg_type);
}
?>
<div class="view_room"></div>
尝试如下:
$.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> ');
} 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>
</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);}
});
}
});
php for pg_name:
while($row=mysql_fetch_assoc($sql)){
$row['pg_name'] = Getpgname($row['pg_id']);
$data[]= $row;
}
或使用连接查询同时返回名称和数据
相关文章:
- ZeroClipboard-在复制之前添加到值
- Javascript返回值只在循环中返回一次
- XMLHttpRequest未返回值-状态202
- 根据是否解析了 Promise 从函数返回值
- Angular,函数在(模型)工厂中返回值
- AngularJS错误:提供程序必须从$get工厂方法返回值
- 如何从客户端的数组中获取用户输入和返回值
- Javascript中带有返回值的嵌套函数
- 为变量分配多个nodejs导出返回值时出现问题
- jQuery-通过AJAX调用动态添加具有值的表单元格
- 通过回调从onreadystatechange返回值
- 从Ajax函数返回值
- JavaScript生成器中收益率返回值的解析
- 动态添加的输入元素不会在脚本中返回值
- 如何在 HTML 中打印 javascript 函数的返回值 “添加 {x} +{y}={result}”
- 如何让Math.random()添加到返回值中,而不是替换它们
- 如何从javascript中的DirectMethod中获得返回值(值类型是Ext.Net.Panel)并将其添加到Ex
- 如何在jquery中添加返回值
- 使用javascript返回值,然后将其添加到ruby数据库中
- $.each函数将返回值添加到外部变量