简体   繁体   English

清除默认HTML表格td值而不会丢失CSS

[英]Clear default HTML table td values without losing CSS

I have multiple HTML tables in a submit form with default values. 我在提交表单中有多个具有默认值的HTML表。 These values in the table td are demo values and therefore I want a button to clear all values in 1 table, but keeping the CSS of that table. 表td中的这些值是演示值,因此我想要一个按钮来清除1个表中的所有值,但保留该表的CSS。 This is my table: 这是我的表:

HTML table: HTML表格:
HTML表格

I use this code for empty the table: 我使用此代码清空表:

updated code 更新的代码

<button>Empty</button>
  <script>
    $( "button" ).click(function() {
        $("#table1").find("td").empty();
    });
</script>

But when I use this code, my table looks like this: 但是当我使用这段代码时,我的表看起来像这样:

HTML empty table: HTML空表:

empty HTML table 空HTML表

I also tried this: 我也试过这个:

document.getElementById("flagTotal").innerHTML = "";

But then the "empty" button wants to submit the form instead of the submit button....? 但是“空”按钮想要提交表单而不是提交按钮....?

Can someone provide me with a solution/ tip? 有人能为我提供解决方案/提示吗?

Update 更新

Minimal code: 最小代码:

div class="table-responsive">
            <table id="table1" class="table table-sw table-bordered">
                <tbody id="tablekrw">
                <tr id="tablehead"><th>Sw</th><th>Krw</th></tr>
                <tr><td><input type="text" class="td-sw" name="sw1[]" value="0.15" id="default-td"></td>
                    <td><input class="td-sw" type="text" name="krw[]" value="0.0"></td></tr>
                <tr><td><input type="text" class="td-sw" name="sw1[]" value="0.18" id="default-td"></td>
                    <td><input class="td-sw" type="text" name="krw[]" value="3.00E-05"></td></tr>
                <tr><td><label><button>Empty</button></label></td></tr>
</table>

  <table id="table-right" class="table table-bordered">
                <tr><td><input class="btn btn-primary" type="submit" value="Submit" name="submit"></td></tr>
                <tr><td colspan="2" class="td"><label for="reset" class="label">Reset this form to original
                            values:</label></td>
                    <td><input class="btn btn-primary" type="reset" value="Reset"</tr>
            </table>

Error message 错误信息

You can clean the table using the querySelectorAll() method from javascript by just using a foreach loop as the following: 您可以使用javascript中的querySelectorAll()方法清理表,只需使用foreach循环,如下所示:

function cleanTable() {
    var fields = document.querySelectorAll("#table td");

    fields.forEach(cell => {
      cell.innerHTML = "";
    });
  }

If you're not familiar with the querySelector or querySelectorAll here is the documentation: https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll . 如果您不熟悉querySelector或querySelectorAll,请参阅以下文档: https//developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll But I tested the code above and it worked. 但我测试了上面的代码并且它有效。

Call this function when clicking in the empty button, and just make sure your title cells are using the th tag and not the td . 单击空按钮时调用此函数,只需确保标题单元格使用th标记而不是td

And also, I believe that if you do not set any width or height for your cells the table will just collapse (but I am not a hundred percent sure, it depends on your css and the frameworks you're using). 而且,我相信如果你没有为你的单元格设置任何宽度或高度,那么表格就会崩溃(但我不是百分百肯定,这取决于你的css和你正在使用的框架)。

Hope it can help! 希望它可以帮助!

I found an answer to my problem. 我找到了问题的答案。 JavaScript: JavaScript的:

function emptyTable() {
$('#empty1').click( function() {
    $(this).closest('table').find('input').val('');
});
}

HTML: HTML:

<tr><td><button type="button" id="empty1">empty Sw, Krw table</button></td></tr>

<script>emptyTable();</script>

Now the table td values dissapear without the table collapsing. 现在表td值消失,而表没有折叠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM