首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >引导表如何隐藏,当使用javascript切换可见时,它没有正确显示

引导表如何隐藏,当使用javascript切换可见时,它没有正确显示
EN

Stack Overflow用户
提问于 2016-10-14 16:34:44
回答 3查看 10.5K关注 0票数 1

我有一张鞋带桌子。我最初使用css隐藏一个表行。如果选中了隐藏表行,则有一个复选框要显示它,否则不应该显示该行。我让它正常工作,但是当选中复选框时,现在可见的行的格式(宽度)不正确(不对齐)。我试着使用这个隐藏行的css (宽度,显示内联表.)但我不能让它起作用。

CSS:

代码语言:javascript
复制
 #hiddenRow {
       display: none;
    }
    .orderTotal {
       padding: 10px;
       background-color: #fdfbe4;
       width: 95%;
       margin: 0 auto;  
    }
    .orderTotal h4 {
       font-weight: bold;   
    }
    .totalOrder {
       color: #ee7a23;
       font-weight: bold;
       font-size: 18px; 
    }
    .totalAmount {
       font-weight: bold;
       font-size: 18px; 
    }

表html:

代码语言:javascript
复制
<div class="orderTotal">
   <h4>Order Total:</h4

   <table id="tableOrderTotal" class="table tableTotal">
    <tbody>
       <tr>
         <td>Item1</td>
         <td class="price-1">50</td>
       </tr>
       <tr id="hiddenRow">
         <td>Item2</td>
         <td class="price-2">13</td>
       </tr>
       <tr>
        <td>Item3</td>
        <td class="price-3">30</td>
       </tr>
       <tr class="summary">
        <td class="totalOrder">Total:</td>
        <td id="result" class="totalAmount"></td>
       </tr>

     </tbody>
   </table>
 </div>

JS显示隐藏的表行:

代码语言:javascript
复制
// hide table row from order total if not checked
$("input[name='product_CD']").on("click", function() {
    $("#hiddenRow").toggleClass('show');
});

下面是选中复选框时行显示方式的屏幕截图。

注意行和价格列的宽度是如何不对齐的。如果该行最初没有隐藏,它会预览,并且在检查元素和手动切换#hiddenRow display:none css时,它也会在浏览器中切换可见/隐藏的精细。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-10-14 17:01:31

我做了个小摆弄:https://jsfiddle.net/ov7d9rLa/

我认为问题可能是由于使用引导和CSS隐藏/显示之间的斗争。我使用引导类“隐藏”简化了这一点,并将该类与click事件切换。

代码语言:javascript
复制
<div class="orderTotal">
   <h4>Order Total:</h4>

   <table id="tableOrderTotal" class="table tableTotal">
    <tbody>
       <tr>
         <td>Item1</td>
         <td class="price-1">50</td>
       </tr>
       <tr id='hiddenRow' class="hidden">
         <td>Item2</td>
         <td class="price-2">13</td>
       </tr>
       <tr>
        <td>Item3</td>
        <td class="price-3">30</td>
       </tr>
       <tr class="summary">
        <td class="totalOrder">Total:</td>
        <td id="result" class="totalAmount"></td>
       </tr>

     </tbody>
   </table>
 </div>

 <button id='toggle' class='btn btn-primary'>
Toggle
</button>

联署材料:

代码语言:javascript
复制
$("#toggle").on("click", function() {
    $("#hiddenRow").toggleClass("hidden");
});
票数 2
EN

Stack Overflow用户

发布于 2016-10-14 16:48:44

我不会使用所有的css。在我看来,jQuery使它清洁了100倍。我会这样做:

我会利用jQuery的hide()toggle()。就像这样:

代码语言:javascript
复制
$(document).ready(function() {

  // Hide the row when the page loads
  $("#hiddenRow").hide();

  // when the user clicks the checkbox, toggle the row
  $("#toggleCheck").click(function() {

    $("#hiddenRow").toggle();

  })

});

这是一个完整的JSBin:http://jsbin.com/nokusunamo/edit?html,js,console,output

票数 2
EN

Stack Overflow用户

发布于 2016-10-14 16:49:44

工作小提琴

只需按@Louys以上评论中建议的那样使用以上评论

代码语言:javascript
复制
#hiddenRow.show{
     display: table-row;
}

希望这能有所帮助。

代码语言:javascript
复制
$("input[name='product_CD']").on("click", function() {
  $("#hiddenRow").toggleClass('show');
});
代码语言:javascript
复制
#hiddenRow {
  display: none;
}
.orderTotal {
  padding: 10px;
  background-color: #fdfbe4;
  width: 95%;
  margin: 0 auto;  
}
.orderTotal h4 {
  font-weight: bold;   
}
.totalOrder {
  color: #ee7a23;
  font-weight: bold;
  font-size: 18px; 
}
.totalAmount {
  font-weight: bold;
  font-size: 18px; 
}

#hiddenRow.show{
  display: table-row;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input name='product_CD' type="checkbox" />
<div class="orderTotal">
  <h4>Order Total:</h4>

  <table id="tableOrderTotal" class="table tableTotal">
    <tbody>
      <tr>
        <td>Item1</td>
        <td class="price-1">50</td>
      </tr>
      <tr id="hiddenRow">
        <td>Item2</td>
        <td class="price-2">13</td>
      </tr>
      <tr>
        <td>Item3</td>
        <td class="price-3">30</td>
      </tr>
      <tr class="summary">
        <td class="totalOrder">Total:</td>
        <td id="result" class="totalAmount"></td>
      </tr>

    </tbody>
  </table>
</div>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40048022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档