javascript - 如何使用javascript切换小数和四舍五入的整数

标签 javascript jquery ajax

我有一个表,它使用 PostgreSQL 数据库呈现来 self 的 Django 应用程序的数据。如何添加一个按钮来切换“分数”列,以便它显示原始小数值和四舍五入整数之间的数字?

下面是一个示例:https://jsfiddle.net/8a66gtww/

<table class="table table-bordered table-condensed table-striped">
    <thead>
        <tr>
            <th class="track_id"><input id="checkAll" type="checkbox" /></th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Score</th>
        </tr>
    </thead>
    <tbody>
        <tr class="odd">
            <td class="track_id"><input type="checkbox" name="track_id" value="2" /></td>
            <td>John</td>
            <td>Do</td>
            <td>65.85</td>
        </tr>
        <tr class="even">
            <td class="track_id"><input type="checkbox" name="track_id" value="1" /></td>
            <td>Michael</td>
            <td>Smith</td>
            <td>88.25</td>
        </tr>
        <tr class="odd">
            <td class="track_id"><input type="checkbox" name="track_id" value="4" /></td>
            <td>Donald</td>
            <td>James</td>
            <td>120.11</td>
        </tr>
    </tbody>
    <tfoot></tfoot>
</table>
<br />
<button onclick="myFunction()">Switch</button> // Switches score between decimal and rounded whole number

最佳答案

看看是否有帮助。我只是稍微修改了你的 html 来存储你的得分值,这样它就可以撤消回合。

<table class="table table-bordered table-condensed table-striped">
    <thead>
        <tr>
            <th class="track_id"><input id="checkAll" type="checkbox" /></th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Score</th>
        </tr>
    </thead>
    <tbody>
        <tr class="odd">
            <td class="track_id"><input type="checkbox" name="track_id" value="2" /></td>
            <td>John</td>
            <td>Do</td>
            <td>65.85</td>
            <input type="hidden" value="65.85">
        </tr>
        <tr class="even">
            <td class="track_id"><input type="checkbox" name="track_id" value="1" /></td>
            <td>Michael</td>
            <td>Smith</td>
            <td>88.25</td>
            <input type="hidden" value="88.25">
        </tr>
        <tr class="odd">
            <td class="track_id"><input type="checkbox" name="track_id" value="4" /></td>
            <td>Donald</td>
            <td>James</td>
            <td>120.11</td>
            <input type="hidden" value="120.11">
        </tr>
    </tbody>
    <tfoot></tfoot>
</table>
<br />
<button class="switch">Switch</button> 

jquery 是:

$('.switch').click(function () {
  $('.table tbody tr td:nth-child(4)').each(function() {

    if ($(this).html().indexOf('.') >= 0) {
        $(this).html(Math.round($(this).html()));
    } else {
        $(this).html($(this).parent().find('input[type=hidden]').val());
    }
  });
});

它验证您的分数是否有小数点。如果分数是小数,则四舍五入。如果分数不是小数,则会恢复原始值。

https://jsfiddle.net/jdm3eovz/

关于javascript - 如何使用javascript切换小数和四舍五入的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37354440/

相关文章:

javascript - 从该特定父级查找兄弟类来执行事件

javascript - JQuery get() + 数组访问会破坏链条吗?

asp.net - 奇怪的 Ajax ComboBox 下拉列表

javascript - ajax post选择器作为opencart "add to cart"按钮中的数据

javascript - UIWebView - 调试 javascript

javascript - 在输入中选择文本并删除不更改输入值( Angular )

php - 使用 php 在自定义内容中创建动态 'Read More' 链接

javascript - XMLHttpRequest - event.loaded 和 event.total 值的问题

javascript - Nodemailer:ERR_CONNECTION_RESET 与 AngularJS 和 NodeJS 与 POST

javascript - Isotope 和 Masonry jQuery 插件之间的区别