如何将一个div的隐藏数据显示到另一个div?

时间:2017-07-04 05:09:54

标签: php jquery codeigniter

我正在处理一个项目,我遇到的地方,当我点击加号时,数据将隐藏,并显示在旁边的div中。 这是代码:

<fieldset class="col-md-4" >
    <legend>Services</legend>
    <div class="col-md-12" >
        <?php
            $id = 0;
            foreach ($servicesname as $val) {
            $id++;
        ?>
        <div class="col-md-12" style="font-size: 16px;" id="itemservices<?php echo $id ?>">
            <span style="float:left;" ><?php echo $val[0]['servicename']; ?></span>
            <a  style="float:right;" href onclick="return addSrvToCart('itemservices<?php echo $id ?>')" >
                <strong>&#8377 <?php echo $val[0]['amount']; ?></strong>
                <span class="glyphicon glyphicon-plus-sign" id="id_<?php echo $id; ?>"></span>
            </a>
        </div>

        <?php } ?>
    </div>
</fieldset>

和下一个div的代码:

<fieldset class="col-md-4" >
    <legend>Cart</legend>
    <div style="list-style:none;" class="no-left-padding" id="cart">
        <div class="col-md-12" >

        </div>
    </div>

    <div class="col-sm-12 no-right-padding" style="background-color:#f3f0f0; padding-top:6px; border:1px solid#ccc">
        <label class="pull-right">/*what should i write here to show the sum */: ₨&nbsp;</label>
    </div>
    <button style="margin-top:10px;" class="btn btn-primary btn-sm pull-right" onclick="bookNowAfterFilter()">Book Now</button>
</fieldset>

这是Jquery代码:

<script>
 function addSrvToCart(elem){
    alert($('#' + elem).html());
    $('#' + elem).hide();

   //return false;
  //what should i write here the show that hidden div   
  document.getElementById('summery);
  return false;
 }
</script>

这是图片,我想在Cart div中显示数据并显示金额

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要更改HTML和脚本以实现结果

试试这样 的 HTML

<fieldset class="col-md-4" >
    <legend>Services</legend>
    <div class="col-md-12" >
        <?php
            $id = 0;
            foreach ($servicesname as $val) {
            $id++;
        ?>
        <div class="col-md-12" style="font-size: 16px;" id="itemservices<?php echo $id ?>">
            <span style="float:left;" ><?php echo $val[0]['servicename']; ?></span>
            <a  style="float:right;" onclick=" addSrvToCart('itemservices<?php echo $id ?>')" >
                &#8377<strong> <?php echo $val[0]['amount']; ?></strong>
                <span class="glyphicon glyphicon-plus-sign" id="id_<?php echo $id; ?>"></span>
            </a>
        </div>

        <?php } ?>
    </div>
</fieldset>

和下一个div的代码:

<fieldset class="col-md-4" >
    <legend>Cart</legend>
    <div style="list-style:none;" class="no-left-padding">
        <div class="col-md-12"  id="cart" >

        </div>
    </div>

    <div class="col-sm-12 no-right-padding pull-right"  style="background-color:#f3f0f0; padding-top:6px; border:1px solid#ccc">
            Total: ₨&nbsp;<label class="" id="sumAmount">0</label>
        </div>
    <button style="margin-top:10px;" class="btn btn-primary btn-sm pull-right" onclick="bookNowAfterFilter()">Book Now</button>
</fieldset>

<强>脚本:

function addSrvToCart(elem){
    var div=$('#' + elem);
    div.hide();
    $('#cart').append(div.html());
    var total = parseInt(div.find('strong').html()) + parseInt($('#sumAmount').html());
    $('#sumAmount').html(total);
}

它将产生输出

enter image description here

我认为它会对你有所帮助。