Onclick打开并关闭div错误

时间:2016-10-07 06:33:50

标签: javascript php jquery html css

我正在创建一个生成数组的Web服务。我希望div打开和关闭。我有一个div点击应该打开另一个div但在我的情况下,当我加载我的页面时,默认情况下打开所有div。所有我希望div只在点击它应该打开时保持关闭。我想点击class="innr-spl3 kmore"ul class="details"即可打开,再点击它就会关闭。

PHP

      <?php if(count($json['resultList']) > 0): ?>
      <?php 
        foreach ($json['resultList'] as $key=>$value) {
            if($json["resultList"][$key]["interestRateMin"] == $json["resultList"][$key]["interestRateMax"]){
                $interest = $json["resultList"][$key]["interestRateMin"];
            }
            else{
                $interest = $json["resultList"][$key]["interestRateMin"].' - '.$json["resultList"][$key]["interestRateMax"];

}

            if($json["resultList"][$key]["financingPercentageMin"] == $json["resultList"][$key]["financingPercentageMax"]){
                $financing = $json["resultList"][$key]["financingPercentageMin"];
            }
            else{
                $financing = $json["resultList"][$key]["financingPercentageMin"].' - '.$json["resultList"][$key]["financingPercentageMax"];

}

                echo '
  <div class="cr-content">                
                <div class="bank-rates">
      <ul>
   <li>
   <div class="innr-spl" style="padding: 5px 0;">
          <span style="display:block;"><img src="'.$json["resultList"][$key]["imageUrl"].'"></span>
   <span class="hide">'.$json["resultList"][$key]["bankName"].'</span>
   </div>
   </li>

 <li><div class="innr-spl2">' . $interest . '%' . '</div></li>
 <li><div class="innr-spl2">' . $financing  . '</div></li>
   <li class="hide"><div class="innr-spl3 kmore" id="'.$key.'"><i class="fa fa-chevron-circle-down" aria-hidden="true"></i>View Datails</div> </li>  
      </ul>
      <div style="clear:both"></div>

      <ul class="details" id="ban_'.$key.'">
        <li><i class="fa fa-thumbs-o-up" aria-hidden="true"></i> Maximum Loan Amount : '.$json["resultList"][$key]["maxLoanAmt"].'</li>
        <li><i class="fa fa-thumbs-o-up" aria-hidden="true"></i> Max Tenure (In Years) : '.$json["resultList"][$key]["repaymentTenureInYears"].'</li>
        <li><i class="fa fa-thumbs-o-up" aria-hidden="true"></i> Interest Rate Type : '.$json["resultList"][$key]["rateType"].'</li>    
      </ul>

    </div>
    </div>

                ';
                $i++;
        }

    ?>
<?php endif;?>

JS

$(document).ready(function(){

    $('.kmore').click(function(){
        var name = this.id;
        //alert(name);
        $("#ban_" + name).slideToggle("fast");
    });

});

2 个答案:

答案 0 :(得分:2)

使用.attr()获取ID

当页面准备就绪时,hide()默认为ul.details

$(document).ready(function(){
    $('.details').hide();
    $('.kmore').click(function(){
         var name = $(this).attr('id');
         //alert(name);
         $("#ban_" + name).toggle();
    });
});

请使用console.log()

确保您使用正确的ID获取正确的元素

答案 1 :(得分:2)

您需要做两件事: -

  1. <ul class="details" id="ban_'.$key.'" style = "display:none;">

  2. var name = $(this).attr('id'); // use attr()

  3. 注意: - 其余的代码对我来说似乎很好。