如何使用javascript切换十进制和四舍五入整数
我有一个表,用于呈现使用PostgreSQL数据库的Django应用程序中的数据。我如何添加一个按钮来切换分数列,使其显示原始十进制值和四舍五入整数之间的数字 下面是一个示例,展示了它的外观:如何使用javascript切换十进制和四舍五入整数,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个表,用于呈现使用PostgreSQL数据库的Django应用程序中的数据。我如何添加一个按钮来切换分数列,使其显示原始十进制值和四舍五入整数之间的数字 下面是一个示例,展示了它的外观: 名字 姓 分数 约翰 做 65.85 迈克尔 史密斯 88.25 唐纳德 詹姆斯 120.11 开关//在小数和四舍五入整数之间切换分数 以下是一些让您开始学习的内容: <script> window.switcher = { johnsScore: 0 } switcher.sw
名字
姓
分数
约翰
做
65.85
迈克尔
史密斯
88.25
唐纳德
詹姆斯
120.11
开关//在小数和四舍五入整数之间切换分数
以下是一些让您开始学习的内容:
<script>
window.switcher = {
johnsScore: 0
}
switcher.switch = function(){
var $johnsScore = $('tbody > tr td').eq(3)
var $johnsCheckbox = $('tbody > tr input')
if ($johnsCheckbox.prop('checked')) {
if (this.johnsScore > 0) {
$johnsScore.text(this.johnsScore)
this.johnsScore = 0
}
else {
this.johnsScore = $johnsScore.text()
$johnsScore.text(Math.round(this.johnsScore))
}
}
}
</script>
window.switcher={
约翰分数:0
}
switcher.switch=函数(){
变量$johnscore=$('tbody>trtd')。等式(3)
变量$johnsCheckbox=$('tbody>tr input')
如果($johnsCheckbox.prop('checked')){
如果(this.johnscore>0){
$johnscore.text(this.johnscore)
此.johnscore=0
}
否则{
this.johnscore=$johnscore.text()
$johnscore.text(Math.round(this.johnscore))
}
}
}
然后,该按钮变为:
<button onclick="switcher.switch()">Switch</button>
开关
看看是否有帮助。我只是稍微修改了你的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>
它验证您的分数是否有小数点。如果分数是小数,则将其四舍五入。如果分数不是小数,则恢复原始值
选择正确的td元素,提取值,四舍五入,然后写回。
<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>
$('.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());
}
});
});