dqmhgz5848 2016-09-29 04:26
浏览 79

如何在jquery中显示不同的颜色按钮

Here i did all correct after return the datas in index page display in that value i need to do one more thing extra what is means while returning data booking_status=="1" means i want to show red color button and button is unclickable(class="btn btn-primary) suppose " booking_status=="0" means i want to show green color button(class="btn btn-primary).in console.log(res); i am getting like this

count:2
data:Array[2]
0:Object
1:Object

booking_status:"1"

id:"2"

pg_id:"1"

rent:"4000"

room_number:"Room 2"

room_sharing:"2"


----------


booking_status:"1"

id:"3"

pg_id:"1"

rent:"4000"

room_number:"Room 3"

room_sharing:"2"

<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);
            $.each(res.data, function(key, value) {
        var booking_status = value.booking_status;
        console.log(booking_status);//i am getting 1 here
        
        if(booking_status == "1"){
             $("#book").removeClass("btn-success").addClass("btn-danger");
             $("#book1").removeClass("btn-success").addClass("btn-danger");
             el = $('[data-id='+value.pg_name +']');
            
             if (el.length) 
             {
                 // console.log(booking_status);
                
                 el.find('.btnmar').append(' <a href="register.php?roomid='+value.id +'&&pg_id='+value.pg_id+'&&amount='+value.room_rent+'&&room_number='+value.room_number+'&&advance='+value.advance_amount+'"><button type="button" class="btn btn-success" id="book" style=" width: 71px; ">'+value.room_number+'</button></a>&nbsp;&nbsp;');
              } 
              else 
              {
                    //console.log(booking_status);
                     var htmlString =  '<div id="toggle" data-id="'+value.pg_name +'"> <div class="container" style=" margin-bottom: 30px;"><div class="row"><h4 style="margin-left:15px;">'+value.pg_name +'</h4><div class="col-sm-10"><div class="btn-group btnmar"><a href="register.php?roomid='+value.id +'&&pg_id='+value.pg_id+'&&amount='+value.room_rent+'&&room_number='+value.room_number+'&&advance='+value.advance_amount+'"><button type="button" class="btn btn-success" style=" width: 71px; id="book1"">'+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);
              }
        }
        
         
        });
            }
          });
    
    }
</script>

pg_details.php

<?php
include_once("admin/config.php");
include("functions.php");
$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)){
        $row['pg_name'] = Getpgname($row['pg_id']);
        $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>

</div>
  • 写回答

2条回答 默认 最新

  • douduan3203 2016-09-29 04:33
    关注

    The basic logic of this is as such, for jquery plus bootstrap:

    $("#idFieldName").removeClass("btn-primary").addClass("btn-success");
    

    What this does is:

    This determines what id="fieldname"

    $("#idFieldName")
    

    This removes the current btn-primary:

    removeClass("btn-primary")
    

    This adds the btn-success:

    removeClass("btn-success")
    

    and the two periods in-between are basically "keep doing this next thing" kind of statement.

    You will need to obviously deal with the if/else logic for your booking_status but that is the easier part.

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧