gpt4 book ai didi

javascript - 将列的值从一个表传输到另一个表

转载 作者:行者123 更新时间:2023-11-28 04:34:29 25 4
gpt4 key购买 nike

我希望通过单击按钮将我表的“总体”列值添加到第二个表中的下一个可用列,我在 SQL 和 My SQL 中看到了与此类似的其他问题,但我不确定如何在 jQuery 中执行此操作。这是我的代码:

$(document).ready(function() {
$(':radio').change(function() {
var row = $(this).closest('.item');
var checkedItems = row.find(":checked")
if (checkedItems.length == 5) {
row.find("td.overall").html(getOverall(checkedItems));
}
})

function getOverall(_checkedItems) {
var total = 0;
_checkedItems.each(function() {
total += parseFloat($(this).val());
});
return total;
}


});
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 95%;
}
td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
text-align: center
}
tr:nth-child(even) {
background-color: #dddddd;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table align=center>
<thead>
<tr>
<th>Skating</th>
<th>Shooting</th>
<th>Passing</th>
<th>Puck Control</th>
<th>Team Play</th>
<th>Overall</th>
</tr>
</thead>
<tbody>
<tr class="item" data-id="1">
<td>
<form action="">
<input type="radio" name="skating" value="1">1
<input type="radio" name="skating" value="2">2
<input type="radio" name="skating" value="3">3
<input type="radio" name="skating" value="4">4
<input type="radio" name="skating" value="5">5
<br>
<input type="radio" name="skating" value="6">6
<input type="radio" name="skating" value="7">7
<input type="radio" name="skating" value="8">8
<input type="radio" name="skating" value="9">9
<input type="radio" name="skating" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="shooting" value="1">1
<input type="radio" name="shooting" value="2">2
<input type="radio" name="shooting" value="3">3
<input type="radio" name="shooting" value="4">4
<input type="radio" name="shooting" value="5">5
<br>
<input type="radio" name="shooting" value="6">6
<input type="radio" name="shooting" value="7">7
<input type="radio" name="shooting" value="8">8
<input type="radio" name="shooting" value="9">9
<input type="radio" name="shooting" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="passing" value="1">1
<input type="radio" name="passing" value="2">2
<input type="radio" name="passing" value="3">3
<input type="radio" name="passing" value="4">4
<input type="radio" name="passing" value="5">5
<br>
<input type="radio" name="passing" value="6">6
<input type="radio" name="passing" value="7">7
<input type="radio" name="passing" value="8">8
<input type="radio" name="passing" value="9">9
<input type="radio" name="passing" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="puck_control" value="1">1
<input type="radio" name="puck_control" value="2">2
<input type="radio" name="puck_control" value="3">3
<input type="radio" name="puck_control" value="4">4
<input type="radio" name="puck_control" value="5">5
<br>
<input type="radio" name="puck_control" value="6">6
<input type="radio" name="puck_control" value="7">7
<input type="radio" name="puck_control" value="8">8
<input type="radio" name="puck_control" value="9">9
<input type="radio" name="puck_control" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="team_play" value="1">1
<input type="radio" name="team_play" value="2">2
<input type="radio" name="team_play" value="3">3
<input type="radio" name="team_play" value="4">4
<input type="radio" name="team_play" value="5">5
<br>
<input type="radio" name="team_play" value="6">6
<input type="radio" name="team_play" value="7">7
<input type="radio" name="team_play" value="8">8
<input type="radio" name="team_play" value="9">9
<input type="radio" name="team_play" value="10">10
</form>
</td>
<td class="overall">
</td>

</tr>
<tr class="item" data-id="2">
<td>
<form action="">
<input type="radio" name="skating" value="1">1
<input type="radio" name="skating" value="2">2
<input type="radio" name="skating" value="3">3
<input type="radio" name="skating" value="4">4
<input type="radio" name="skating" value="5">5
<br>
<input type="radio" name="skating" value="6">6
<input type="radio" name="skating" value="7">7
<input type="radio" name="skating" value="8">8
<input type="radio" name="skating" value="9">9
<input type="radio" name="skating" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="shooting" value="1">1
<input type="radio" name="shooting" value="2">2
<input type="radio" name="shooting" value="3">3
<input type="radio" name="shooting" value="4">4
<input type="radio" name="shooting" value="5">5
<br>
<input type="radio" name="shooting" value="6">6
<input type="radio" name="shooting" value="7">7
<input type="radio" name="shooting" value="8">8
<input type="radio" name="shooting" value="9">9
<input type="radio" name="shooting" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="passing" value="1">1
<input type="radio" name="passing" value="2">2
<input type="radio" name="passing" value="3">3
<input type="radio" name="passing" value="4">4
<input type="radio" name="passing" value="5">5
<br>
<input type="radio" name="passing" value="6">6
<input type="radio" name="passing" value="7">7
<input type="radio" name="passing" value="8">8
<input type="radio" name="passing" value="9">9
<input type="radio" name="passing" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="puck_control" value="1">1
<input type="radio" name="puck_control" value="2">2
<input type="radio" name="puck_control" value="3">3
<input type="radio" name="puck_control" value="4">4
<input type="radio" name="puck_control" value="5">5
<br>
<input type="radio" name="puck_control" value="6">6
<input type="radio" name="puck_control" value="7">7
<input type="radio" name="puck_control" value="8">8
<input type="radio" name="puck_control" value="9">9
<input type="radio" name="puck_control" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="team_play" value="1">1
<input type="radio" name="team_play" value="2">2
<input type="radio" name="team_play" value="3">3
<input type="radio" name="team_play" value="4">4
<input type="radio" name="team_play" value="5">5
<br>
<input type="radio" name="team_play" value="6">6
<input type="radio" name="team_play" value="7">7
<input type="radio" name="team_play" value="8">8
<input type="radio" name="team_play" value="9">9
<input type="radio" name="team_play" value="10">10
</form>
</td>
<td class="overall">
</td>

</tr>
</tbody>
</table>

最佳答案

如果我完全理解您的要求,就是如何在第二个表的整体列上打印整体。我建议你做的是将 total 变量打印到 td 和整个类。

当你这样做时:
如果(checkedItems.length == 5){
row.find("td.overall").html(getOverall(checkedItems));
}
查找功能,如 jquery documentation to find function 中所述,

Given a jQuery object that represents a set of DOM elements, the .find() method allows us to search through the descendants of these elements in the DOM tree and construct a new jQuery object from the matching elements.

所以您需要做的就是使用 $(selector); 在文档中搜索 td.overall;

检查一下:

$(document).ready(function() {
$(':radio').change(function() {
var row = $(this).closest('.item');
var checkedItems = row.find(":checked")
if (checkedItems.length == 5) {
$("td.overall").html(getOverall(checkedItems));
}
})

function getOverall(_checkedItems) {
var total = 0;
_checkedItems.each(function() {
total += parseFloat($(this).val());
});
return total;
}


});
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 95%;
}
td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
text-align: center
}
tr:nth-child(even) {
background-color: #dddddd;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table align=center>
<thead>
<tr>
<th>Skating</th>
<th>Shooting</th>
<th>Passing</th>
<th>Puck Control</th>
<th>Team Play</th>
<th>Overall</th>
</tr>
</thead>
<tbody>
<tr class="item" data-id="1">
<td>
<form action="">
<input type="radio" name="skating" value="1">1
<input type="radio" name="skating" value="2">2
<input type="radio" name="skating" value="3">3
<input type="radio" name="skating" value="4">4
<input type="radio" name="skating" value="5">5
<br>
<input type="radio" name="skating" value="6">6
<input type="radio" name="skating" value="7">7
<input type="radio" name="skating" value="8">8
<input type="radio" name="skating" value="9">9
<input type="radio" name="skating" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="shooting" value="1">1
<input type="radio" name="shooting" value="2">2
<input type="radio" name="shooting" value="3">3
<input type="radio" name="shooting" value="4">4
<input type="radio" name="shooting" value="5">5
<br>
<input type="radio" name="shooting" value="6">6
<input type="radio" name="shooting" value="7">7
<input type="radio" name="shooting" value="8">8
<input type="radio" name="shooting" value="9">9
<input type="radio" name="shooting" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="passing" value="1">1
<input type="radio" name="passing" value="2">2
<input type="radio" name="passing" value="3">3
<input type="radio" name="passing" value="4">4
<input type="radio" name="passing" value="5">5
<br>
<input type="radio" name="passing" value="6">6
<input type="radio" name="passing" value="7">7
<input type="radio" name="passing" value="8">8
<input type="radio" name="passing" value="9">9
<input type="radio" name="passing" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="puck_control" value="1">1
<input type="radio" name="puck_control" value="2">2
<input type="radio" name="puck_control" value="3">3
<input type="radio" name="puck_control" value="4">4
<input type="radio" name="puck_control" value="5">5
<br>
<input type="radio" name="puck_control" value="6">6
<input type="radio" name="puck_control" value="7">7
<input type="radio" name="puck_control" value="8">8
<input type="radio" name="puck_control" value="9">9
<input type="radio" name="puck_control" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="team_play" value="1">1
<input type="radio" name="team_play" value="2">2
<input type="radio" name="team_play" value="3">3
<input type="radio" name="team_play" value="4">4
<input type="radio" name="team_play" value="5">5
<br>
<input type="radio" name="team_play" value="6">6
<input type="radio" name="team_play" value="7">7
<input type="radio" name="team_play" value="8">8
<input type="radio" name="team_play" value="9">9
<input type="radio" name="team_play" value="10">10
</form>
</td>
<td class="overall">
</td>

</tr>
<tr class="item" data-id="2">
<td>
<form action="">
<input type="radio" name="skating" value="1">1
<input type="radio" name="skating" value="2">2
<input type="radio" name="skating" value="3">3
<input type="radio" name="skating" value="4">4
<input type="radio" name="skating" value="5">5
<br>
<input type="radio" name="skating" value="6">6
<input type="radio" name="skating" value="7">7
<input type="radio" name="skating" value="8">8
<input type="radio" name="skating" value="9">9
<input type="radio" name="skating" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="shooting" value="1">1
<input type="radio" name="shooting" value="2">2
<input type="radio" name="shooting" value="3">3
<input type="radio" name="shooting" value="4">4
<input type="radio" name="shooting" value="5">5
<br>
<input type="radio" name="shooting" value="6">6
<input type="radio" name="shooting" value="7">7
<input type="radio" name="shooting" value="8">8
<input type="radio" name="shooting" value="9">9
<input type="radio" name="shooting" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="passing" value="1">1
<input type="radio" name="passing" value="2">2
<input type="radio" name="passing" value="3">3
<input type="radio" name="passing" value="4">4
<input type="radio" name="passing" value="5">5
<br>
<input type="radio" name="passing" value="6">6
<input type="radio" name="passing" value="7">7
<input type="radio" name="passing" value="8">8
<input type="radio" name="passing" value="9">9
<input type="radio" name="passing" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="puck_control" value="1">1
<input type="radio" name="puck_control" value="2">2
<input type="radio" name="puck_control" value="3">3
<input type="radio" name="puck_control" value="4">4
<input type="radio" name="puck_control" value="5">5
<br>
<input type="radio" name="puck_control" value="6">6
<input type="radio" name="puck_control" value="7">7
<input type="radio" name="puck_control" value="8">8
<input type="radio" name="puck_control" value="9">9
<input type="radio" name="puck_control" value="10">10
</form>
</td>
<td>
<form action="">
<input type="radio" name="team_play" value="1">1
<input type="radio" name="team_play" value="2">2
<input type="radio" name="team_play" value="3">3
<input type="radio" name="team_play" value="4">4
<input type="radio" name="team_play" value="5">5
<br>
<input type="radio" name="team_play" value="6">6
<input type="radio" name="team_play" value="7">7
<input type="radio" name="team_play" value="8">8
<input type="radio" name="team_play" value="9">9
<input type="radio" name="team_play" value="10">10
</form>
</td>
<td class="overall">
</td>

</tr>
</tbody>
</table>

关于javascript - 将列的值从一个表传输到另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41556778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com