javascript - Bootstrap : insert dynamically a row in tab

标签 javascript php twitter-bootstrap

我尝试将此脚本插入 Bootstrap 选项卡中。 我的目标是动态构建一个表。

我的问题,每次插入新行时,我的页面都会刷新。

你有办法解决这个问题吗?

<table class="table table-sm table-hover" id="mtable">
    <thead>
        <tr>
            <td>Item</td>
            <td>Red</td>
            <td>Green</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Some Item</td>
            <td><input type="checkbox" /></td>
            <td><input type="checkbox" /></td>
        </tr>
    </tbody>
</table><br /><br />
<input id="row" placeholder="Enter Item Name" />
<button id="irow">Insert Row</button><br /><br />
<input id="col" placeholder="Enter Heading" />
<button id="icol">Insert Column</button>

<script>
      $('#irow').click(function(){
        if($('#row').val()){
          $('#mtable tbody').append($("#mtable tbody tr:last").clone());
          $('#mtable tbody tr:last :checkbox').attr('checked',false);
          $('#mtable tbody tr:last td:first').html($('#row').val());
        }else{alert('Enter Text');}
      });
      $('#icol').click(function(){
        if($('#col').val()){
          $('#mtable tr').append($("<td>"));
          $('#mtable thead tr>td:last').html($('#col').val());
          $('#mtable tbody tr').each(function(){$(this).children('td:last').append($('<input type="checkbox">'))});
        }else{alert('Enter Text');}
      });
</script>

最佳答案

听起来这是在表单内。

更改<button><button type='button'>以防止它提交表单。

<button> 的默认类型是 submit所以刷新是由于表单提交造成的。

如果它与表单无关,您将需要创建一个重现问题的演示

关于javascript - Bootstrap : insert dynamically a row in tab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39735319/

相关文章:

javascript - jQuery 函数永远不会在 .ready() 上执行

javascript - "message"使用 postMessage 时未触发事件

php - 有没有一种简单的方法可以只显示html5音频播放器中的播放和停止按钮?

php - 从表名数组中提取列值

CSS 选项卡不能在 Bootstrap 上使用 anchor ?

javascript - 使用 jQuery 滑动面板上的动态 View

php - 如何获取 Quora 上某些值的 XPath?

javascript - 在 Bootstrap 模式中加载 iframe

html - css 不同高度 div 网格空间

javascript - 给两边的文字等边距