选择 table 个元素并使用 Jquery/Javascript 计算总值
Selecting table elements and calculating total value using Jquery/Javascript
我正在尝试创建一个 yes/no 使用积分系统的调查问卷,它会根据用户的选择自动计算总分。每个是 td 和 no td 都有分配给它的点数,因此当用户完成问卷时,他们应该有一些总点数。
我有困难:
A) 使突出显示功能 select 每行只有是或否;
B) 在每一行的计算函数中只使用 selected 是或否整数——相反,计算函数是对所有带有数字的 TD 求和。
参见代码笔:https://codepen.io/froglegg/pen/yZJGbr
//Javascript/Jquery
//this adds a cursor-pointer over 2nd and 3rd td children of tr
$(document).ready(function() {
$("#countit tr td:nth-child(3), td:nth-child(2)").mouseover(function(e) {
$(this).css("cursor", "pointer");
});
//this adds the function for removing the highlight; what selector should I use to make sure the class is removed from only the current row?
$("#countit tr td:nth-child(3), td:nth-child(2)").click(function(e) {
$("#countit tr td:nth-child(3), td:nth-child(2)").removeClass("highlight");
//this constructs a variable that makes the 2nd and 3rd td children event targets... I think.
var clickedCell= $(e.target).closest("td:nth-child(3), td:nth-child(2)");
// this adds a highlight class to the clickedCell variable constructed above... then outputs the text of that cell as html...
clickedCell.addClass("highlight");
$("#spnText").html(
'Clicked table cell value is: <b> ' + clickedCell.text() + '</b>');
});
});
//this is the summing function I found, constructs a tds variable from td elements. I've tried to add the 'highlight' class to the selector, so that only cells that have been highlighted by the functions above will be calculated, doesn't seem to have an effect, however...
var tds = document.getElementById('countit').getElementsByTagName('td');
var sum = 0;
for(var i = 0; i < tds.length; i ++) {
if(tds[i].className == 'count-me' && 'highlight') {
sum += isNaN(tds[i].innerHTML) ? 0 : parseInt(tds[i].innerHTML);
}
}
//this outputs the sum total of the tds
document.getElementById('countit').innerHTML += '<br><tr><td><div class="section-content">YOUR TOTAL POINTS [' + sum + ']</div></td><td></td><td></td></tr>';
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #ccd;
}
tr:nth-child(even) {
background-color: #dddddd;
}
tr:nth-child(odd) {
background-color: #ddeedd;
}
.highlight {
background-color: Yellow;
color: Green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<h2 class="section-title">Risk Checklist</h2>
<div class="section-content">
<p class="section-content">Use this 11-question checklist to get a better idea of your portfolio’s risk exposure and see where you may be able to make changes that help you generate more ancillary revenue, increase leasing office efficiency and reduce resident-caused risk.
</p>
<table class="table table-striped k-table" id="countit">
<thead>
<tr>
<th> </th>
<th>YES</th>
<th>NO</th>
</tr>
</thead>
<tbody style="">
<tr style="">
<td>1. Do you require liability coverage in your lease at your properties?</td>
<td class="count-me">0</td>
<td class="count-me">75</td>
</tr>
<tr style="">
<td style="">2. If yes, do you have a method of ensuring residents maintain liability coverage throughout the course of their lease?</td>
<td class="count-me">0</td>
<td class="count-me">75</td>
</tr>
<tr style="">
<td>3. If yes, do you have a preferred provider for renters insurance and liability coverage?</td>
<td class="count-me">0</td>
<td class="count-me">10</td>
</tr>
<tr style="">
<td>4. If yes, does your management team and property staff understand what your preferred partner’s program covers?</td>
<td class="count-me">0</td>
<td class="count-me">10</td>
</tr>
<tr style="">
<td>5. Since you or your preferred partner implemented liability coverage requirements, have you had an uninsured loss caused by a resident that resulted in fire, smoke, explosion or water damage to the property?</td>
<td class="count-me">30</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td style="">6. Do residents have easy access to purchase an insurance policy from your preferred partner via website, mobile, phone and/or point of lease enrollment?</td>
<td class="count-me">0</td>
<td class="count-me">5</td>
</tr>
<tr>
<td>7. Do the majority of residents choose your preferred partner’s policies?</td>
<td class="count-me">0</td>
<td class="count-me">5</td>
</tr>
<tr style="">
<td>8. Do you feel your site staff spends too much time managing or following up with residents to ensure they meet their lease requirements for liability coverage?</td>
<td class="count-me">5</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td style="">9. Do you believe you wrote off too much bad debt last year?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td>10. Do you believe that your collections company should be recovering more from delinquent residents?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
<tr>
<td>11. Do you feel that you should get better support, have better preferred partner take rates, more ancillary revenue, and have less site staff workload with your renters insurance program?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
</tbody>
</table>
</div>
</div>
<br/>
<span id="spnText"></span>
在这里,这些更改应该有效。由于您已经在使用 jQuery
我决定通过将所有内容更改为更易于使用的 jQuery
来简化您的代码。
注意:您的 html 也有细微变化。
为了汇总更改,您只是在第一次加载文档时相加,所以我添加了一个求和函数,每次单击 td
时都会调用该函数。此外,如果您想取消select它,我现在允许重新单击突出显示的 td。
jQuery
大大简化的一件事是找到其他元素是多么容易。使用 siblings()
我能够轻松地 select 每个 td 的兄弟姐妹。
最后的变化是,我在你的 html 中添加了总行并向其添加了一个 id
,这样更改它的值就像简单地通过它的 id 调用它一样简单并设置它的文本。
如果您有任何问题,请告诉我。
$(document).ready(function() {
$("#countit tr td:nth-child(3), td:nth-child(2)").mouseover(function(e) {
$(this).css("cursor", "pointer");
});
$('.count-me').on('click', function() {
var sibling = $(this).siblings('.count-me');
if($(sibling).hasClass('highlight')) {
sibling.removeClass('highlight');
}
if($(this).hasClass('highlight')) {
$(this).removeClass('highlight');
} else {
$(this).addClass('highlight');
}
sumTotals();
});
function sumTotals() {
var sum = 0;
$('.count-me').each(function() {
if($(this).hasClass('highlight')) {
sum += parseInt($(this).text());
}
})
$('#total').text(sum);
}
});
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #ccd;
}
tr:nth-child(even) {
background-color: #dddddd;
}
tr:nth-child(odd) {
background-color: #ddeedd;
}
.highlight {
background-color: Yellow;
color: Green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<h2 class="section-title">Risk Checklist</h2>
<div class="section-content">
<p class="section-content">Use this 11-question checklist to get a better idea of your portfolio’s risk exposure and see where you may be able to make changes that help you generate more ancillary revenue, increase leasing office efficiency and reduce resident-caused risk.
</p>
<table class="table table-striped k-table" id="countit">
<thead>
<tr>
<th> </th>
<th>YES</th>
<th>NO</th>
</tr>
</thead>
<tbody style="">
<tr style="">
<td>1. Do you require liability coverage in your lease at your properties?</td>
<td class="count-me">0</td>
<td class="count-me">75</td>
</tr>
<tr style="">
<td style="">2. If yes, do you have a method of ensuring residents maintain liability coverage throughout the course of their lease?</td>
<td class="count-me">0</td>
<td class="count-me">75</td>
</tr>
<tr style="">
<td>3. If yes, do you have a preferred provider for renters insurance and liability coverage?</td>
<td class="count-me">0</td>
<td class="count-me">10</td>
</tr>
<tr style="">
<td>4. If yes, does your management team and property staff understand what your preferred partner’s program covers?</td>
<td class="count-me">0</td>
<td class="count-me">10</td>
</tr>
<tr style="">
<td>5. Since you or your preferred partner implemented liability coverage requirements, have you had an uninsured loss caused by a resident that resulted in fire, smoke, explosion or water damage to the property?</td>
<td class="count-me">30</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td style="">6. Do residents have easy access to purchase an insurance policy from your preferred partner via website, mobile, phone and/or point of lease enrollment?</td>
<td class="count-me">0</td>
<td class="count-me">5</td>
</tr>
<tr>
<td>7. Do the majority of residents choose your preferred partner’s policies?</td>
<td class="count-me">0</td>
<td class="count-me">5</td>
</tr>
<tr style="">
<td>8. Do you feel your site staff spends too much time managing or following up with residents to ensure they meet their lease requirements for liability coverage?</td>
<td class="count-me">5</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td style="">9. Do you believe you wrote off too much bad debt last year?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td>10. Do you believe that your collections company should be recovering more from delinquent residents?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
<tr>
<td>11. Do you feel that you should get better support, have better preferred partner take rates, more ancillary revenue, and have less site staff workload with your renters insurance program?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
</tbody>
<br><tr><td><div class="section-content">YOUR TOTAL POINTS <font id="total">0</font></div></td><td></td><td></td></tr>
</table>
</div>
</div>
<br/>
<span id="spnText"></span>
首先,我删除了您只需将 cursor:pointer
添加到 TD 的代码...可以使用 CSS 完成。
其次,我通过为 $("#countit tr td:nth-child(3), #countit tr td:nth-child(2)");
使用变量减少了 jQuery 查找的数量。注意 #countit tr
的重复...当您使用多个选择器时,它们都是独立的。
第三,我将计数循环放在点击处理程序中...因为您希望在每次点击时更新总数...而不仅仅是在页面加载时。
其余部分请参见代码中的注释。
$(document).ready(function() {
// Use a variable to reduce the amount of jQuery lookups.
var yesNoColumns = $("#countit tr td:nth-child(3), #countit tr td:nth-child(2)");
// Click handler on yes/no
yesNoColumns.click(function(e) {
// $(this) is the clicked TD
var clickedCell= $(this);
// Remove any previou highlights on the clicked row
clickedCell.parent("tr").find("td:nth-child(3), td:nth-child(2)").removeClass("highlight");
// Add the highlight on the clicked cell
clickedCell.addClass("highlight");
// Show the value of the clicked cell
$("#spnText").html(
'Clicked table cell value is: <b> ' + clickedCell.text() + '</b>');
// This looksup for all yes/no cells
var tds = $('#countit').find('td.count-me');
// Count the sum on every click (That why the loop now is in the click handler)
var sum = 0;
for(var i = 0; i < tds.length; i ++) {
if(tds.eq(i).hasClass('count-me') && tds.eq(i).hasClass('highlight') ) {
sum += isNaN(tds.eq(i).text()) ? 0 : parseInt(tds.eq(i).text());
}
}
// This outputs the sum total of the tds.
// Only the .sum SPAN is updated
$('.sum').text(sum);
});
});
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #ccd;
}
tr:nth-child(even) {
background-color: #dddddd;
}
tr:nth-child(odd) {
background-color: #ddeedd;
}
.highlight {
background-color: Yellow;
color: Green;
}
/* Added CSS rule */
#countit tr td:nth-child(3), #countit tr td:nth-child(2){
cursor:pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<br /><br />
<div class="container">
<h2 class="section-title">Risk Checklist</h2>
<div class="section-content">
<p class="section-content">Use this 11-question checklist to get a better idea of your portfolio’s risk exposure and see where you may be able to make changes that help you generate more ancillary revenue, increase leasing office efficiency and reduce resident-caused
risk.
</p>
<table class="table table-striped k-table" id="countit">
<thead>
<tr>
<th> </th>
<th>YES</th>
<th>NO</th>
</tr>
</thead>
<tbody style="">
<tr style="">
<td>1. Do you require liability coverage in your lease at your properties?</td>
<td class="count-me">0</td>
<td class="count-me">75</td>
</tr>
<tr style="">
<td style="">2. If yes, do you have a method of ensuring residents maintain liability coverage throughout the course of their lease?</td>
<td class="count-me">0</td>
<td class="count-me">75</td>
</tr>
<tr style="">
<td>3. If yes, do you have a preferred provider for renters insurance and liability coverage?</td>
<td class="count-me">0</td>
<td class="count-me">10</td>
</tr>
<tr style="">
<td>4. If yes, does your management team and property staff understand what your preferred partner’s program covers?</td>
<td class="count-me">0</td>
<td class="count-me">10</td>
</tr>
<tr style="">
<td>5. Since you or your preferred partner implemented liability coverage requirements, have you had an uninsured loss caused by a resident that resulted in fire, smoke, explosion or water damage to the property?</td>
<td class="count-me">30</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td style="">6. Do residents have easy access to purchase an insurance policy from your preferred partner via website, mobile, phone and/or point of lease enrollment?</td>
<td class="count-me">0</td>
<td class="count-me">5</td>
</tr>
<tr>
<td>7. Do the majority of residents choose your preferred partner’s policies?</td>
<td class="count-me">0</td>
<td class="count-me">5</td>
</tr>
<tr style="">
<td>8. Do you feel your site staff spends too much time managing or following up with residents to ensure they meet their lease requirements for liability coverage?</td>
<td class="count-me">5</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td style="">9. Do you believe you wrote off too much bad debt last year?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td>10. Do you believe that your collections company should be recovering more from delinquent residents?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
<tr>
<td>11. Do you feel that you should get better support, have better preferred partner take rates, more ancillary revenue, and have less site staff workload with your renters insurance program?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
<!-- add the total points row here -->
<tr>
<td>
<div class="section-content">YOUR TOTAL POINTS <span class="sum">0</span></div>
</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
<br/>
<span id="spnText"></span>
也在 CodePen.
我正在尝试创建一个 yes/no 使用积分系统的调查问卷,它会根据用户的选择自动计算总分。每个是 td 和 no td 都有分配给它的点数,因此当用户完成问卷时,他们应该有一些总点数。
我有困难:
A) 使突出显示功能 select 每行只有是或否;
B) 在每一行的计算函数中只使用 selected 是或否整数——相反,计算函数是对所有带有数字的 TD 求和。
参见代码笔:https://codepen.io/froglegg/pen/yZJGbr
//Javascript/Jquery
//this adds a cursor-pointer over 2nd and 3rd td children of tr
$(document).ready(function() {
$("#countit tr td:nth-child(3), td:nth-child(2)").mouseover(function(e) {
$(this).css("cursor", "pointer");
});
//this adds the function for removing the highlight; what selector should I use to make sure the class is removed from only the current row?
$("#countit tr td:nth-child(3), td:nth-child(2)").click(function(e) {
$("#countit tr td:nth-child(3), td:nth-child(2)").removeClass("highlight");
//this constructs a variable that makes the 2nd and 3rd td children event targets... I think.
var clickedCell= $(e.target).closest("td:nth-child(3), td:nth-child(2)");
// this adds a highlight class to the clickedCell variable constructed above... then outputs the text of that cell as html...
clickedCell.addClass("highlight");
$("#spnText").html(
'Clicked table cell value is: <b> ' + clickedCell.text() + '</b>');
});
});
//this is the summing function I found, constructs a tds variable from td elements. I've tried to add the 'highlight' class to the selector, so that only cells that have been highlighted by the functions above will be calculated, doesn't seem to have an effect, however...
var tds = document.getElementById('countit').getElementsByTagName('td');
var sum = 0;
for(var i = 0; i < tds.length; i ++) {
if(tds[i].className == 'count-me' && 'highlight') {
sum += isNaN(tds[i].innerHTML) ? 0 : parseInt(tds[i].innerHTML);
}
}
//this outputs the sum total of the tds
document.getElementById('countit').innerHTML += '<br><tr><td><div class="section-content">YOUR TOTAL POINTS [' + sum + ']</div></td><td></td><td></td></tr>';
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #ccd;
}
tr:nth-child(even) {
background-color: #dddddd;
}
tr:nth-child(odd) {
background-color: #ddeedd;
}
.highlight {
background-color: Yellow;
color: Green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<h2 class="section-title">Risk Checklist</h2>
<div class="section-content">
<p class="section-content">Use this 11-question checklist to get a better idea of your portfolio’s risk exposure and see where you may be able to make changes that help you generate more ancillary revenue, increase leasing office efficiency and reduce resident-caused risk.
</p>
<table class="table table-striped k-table" id="countit">
<thead>
<tr>
<th> </th>
<th>YES</th>
<th>NO</th>
</tr>
</thead>
<tbody style="">
<tr style="">
<td>1. Do you require liability coverage in your lease at your properties?</td>
<td class="count-me">0</td>
<td class="count-me">75</td>
</tr>
<tr style="">
<td style="">2. If yes, do you have a method of ensuring residents maintain liability coverage throughout the course of their lease?</td>
<td class="count-me">0</td>
<td class="count-me">75</td>
</tr>
<tr style="">
<td>3. If yes, do you have a preferred provider for renters insurance and liability coverage?</td>
<td class="count-me">0</td>
<td class="count-me">10</td>
</tr>
<tr style="">
<td>4. If yes, does your management team and property staff understand what your preferred partner’s program covers?</td>
<td class="count-me">0</td>
<td class="count-me">10</td>
</tr>
<tr style="">
<td>5. Since you or your preferred partner implemented liability coverage requirements, have you had an uninsured loss caused by a resident that resulted in fire, smoke, explosion or water damage to the property?</td>
<td class="count-me">30</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td style="">6. Do residents have easy access to purchase an insurance policy from your preferred partner via website, mobile, phone and/or point of lease enrollment?</td>
<td class="count-me">0</td>
<td class="count-me">5</td>
</tr>
<tr>
<td>7. Do the majority of residents choose your preferred partner’s policies?</td>
<td class="count-me">0</td>
<td class="count-me">5</td>
</tr>
<tr style="">
<td>8. Do you feel your site staff spends too much time managing or following up with residents to ensure they meet their lease requirements for liability coverage?</td>
<td class="count-me">5</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td style="">9. Do you believe you wrote off too much bad debt last year?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td>10. Do you believe that your collections company should be recovering more from delinquent residents?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
<tr>
<td>11. Do you feel that you should get better support, have better preferred partner take rates, more ancillary revenue, and have less site staff workload with your renters insurance program?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
</tbody>
</table>
</div>
</div>
<br/>
<span id="spnText"></span>
在这里,这些更改应该有效。由于您已经在使用 jQuery
我决定通过将所有内容更改为更易于使用的 jQuery
来简化您的代码。
注意:您的 html 也有细微变化。
为了汇总更改,您只是在第一次加载文档时相加,所以我添加了一个求和函数,每次单击 td
时都会调用该函数。此外,如果您想取消select它,我现在允许重新单击突出显示的 td。
jQuery
大大简化的一件事是找到其他元素是多么容易。使用 siblings()
我能够轻松地 select 每个 td 的兄弟姐妹。
最后的变化是,我在你的 html 中添加了总行并向其添加了一个 id
,这样更改它的值就像简单地通过它的 id 调用它一样简单并设置它的文本。
如果您有任何问题,请告诉我。
$(document).ready(function() {
$("#countit tr td:nth-child(3), td:nth-child(2)").mouseover(function(e) {
$(this).css("cursor", "pointer");
});
$('.count-me').on('click', function() {
var sibling = $(this).siblings('.count-me');
if($(sibling).hasClass('highlight')) {
sibling.removeClass('highlight');
}
if($(this).hasClass('highlight')) {
$(this).removeClass('highlight');
} else {
$(this).addClass('highlight');
}
sumTotals();
});
function sumTotals() {
var sum = 0;
$('.count-me').each(function() {
if($(this).hasClass('highlight')) {
sum += parseInt($(this).text());
}
})
$('#total').text(sum);
}
});
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #ccd;
}
tr:nth-child(even) {
background-color: #dddddd;
}
tr:nth-child(odd) {
background-color: #ddeedd;
}
.highlight {
background-color: Yellow;
color: Green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<h2 class="section-title">Risk Checklist</h2>
<div class="section-content">
<p class="section-content">Use this 11-question checklist to get a better idea of your portfolio’s risk exposure and see where you may be able to make changes that help you generate more ancillary revenue, increase leasing office efficiency and reduce resident-caused risk.
</p>
<table class="table table-striped k-table" id="countit">
<thead>
<tr>
<th> </th>
<th>YES</th>
<th>NO</th>
</tr>
</thead>
<tbody style="">
<tr style="">
<td>1. Do you require liability coverage in your lease at your properties?</td>
<td class="count-me">0</td>
<td class="count-me">75</td>
</tr>
<tr style="">
<td style="">2. If yes, do you have a method of ensuring residents maintain liability coverage throughout the course of their lease?</td>
<td class="count-me">0</td>
<td class="count-me">75</td>
</tr>
<tr style="">
<td>3. If yes, do you have a preferred provider for renters insurance and liability coverage?</td>
<td class="count-me">0</td>
<td class="count-me">10</td>
</tr>
<tr style="">
<td>4. If yes, does your management team and property staff understand what your preferred partner’s program covers?</td>
<td class="count-me">0</td>
<td class="count-me">10</td>
</tr>
<tr style="">
<td>5. Since you or your preferred partner implemented liability coverage requirements, have you had an uninsured loss caused by a resident that resulted in fire, smoke, explosion or water damage to the property?</td>
<td class="count-me">30</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td style="">6. Do residents have easy access to purchase an insurance policy from your preferred partner via website, mobile, phone and/or point of lease enrollment?</td>
<td class="count-me">0</td>
<td class="count-me">5</td>
</tr>
<tr>
<td>7. Do the majority of residents choose your preferred partner’s policies?</td>
<td class="count-me">0</td>
<td class="count-me">5</td>
</tr>
<tr style="">
<td>8. Do you feel your site staff spends too much time managing or following up with residents to ensure they meet their lease requirements for liability coverage?</td>
<td class="count-me">5</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td style="">9. Do you believe you wrote off too much bad debt last year?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td>10. Do you believe that your collections company should be recovering more from delinquent residents?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
<tr>
<td>11. Do you feel that you should get better support, have better preferred partner take rates, more ancillary revenue, and have less site staff workload with your renters insurance program?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
</tbody>
<br><tr><td><div class="section-content">YOUR TOTAL POINTS <font id="total">0</font></div></td><td></td><td></td></tr>
</table>
</div>
</div>
<br/>
<span id="spnText"></span>
首先,我删除了您只需将 cursor:pointer
添加到 TD 的代码...可以使用 CSS 完成。
其次,我通过为 $("#countit tr td:nth-child(3), #countit tr td:nth-child(2)");
使用变量减少了 jQuery 查找的数量。注意 #countit tr
的重复...当您使用多个选择器时,它们都是独立的。
第三,我将计数循环放在点击处理程序中...因为您希望在每次点击时更新总数...而不仅仅是在页面加载时。
其余部分请参见代码中的注释。
$(document).ready(function() {
// Use a variable to reduce the amount of jQuery lookups.
var yesNoColumns = $("#countit tr td:nth-child(3), #countit tr td:nth-child(2)");
// Click handler on yes/no
yesNoColumns.click(function(e) {
// $(this) is the clicked TD
var clickedCell= $(this);
// Remove any previou highlights on the clicked row
clickedCell.parent("tr").find("td:nth-child(3), td:nth-child(2)").removeClass("highlight");
// Add the highlight on the clicked cell
clickedCell.addClass("highlight");
// Show the value of the clicked cell
$("#spnText").html(
'Clicked table cell value is: <b> ' + clickedCell.text() + '</b>');
// This looksup for all yes/no cells
var tds = $('#countit').find('td.count-me');
// Count the sum on every click (That why the loop now is in the click handler)
var sum = 0;
for(var i = 0; i < tds.length; i ++) {
if(tds.eq(i).hasClass('count-me') && tds.eq(i).hasClass('highlight') ) {
sum += isNaN(tds.eq(i).text()) ? 0 : parseInt(tds.eq(i).text());
}
}
// This outputs the sum total of the tds.
// Only the .sum SPAN is updated
$('.sum').text(sum);
});
});
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #ccd;
}
tr:nth-child(even) {
background-color: #dddddd;
}
tr:nth-child(odd) {
background-color: #ddeedd;
}
.highlight {
background-color: Yellow;
color: Green;
}
/* Added CSS rule */
#countit tr td:nth-child(3), #countit tr td:nth-child(2){
cursor:pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<br /><br />
<div class="container">
<h2 class="section-title">Risk Checklist</h2>
<div class="section-content">
<p class="section-content">Use this 11-question checklist to get a better idea of your portfolio’s risk exposure and see where you may be able to make changes that help you generate more ancillary revenue, increase leasing office efficiency and reduce resident-caused
risk.
</p>
<table class="table table-striped k-table" id="countit">
<thead>
<tr>
<th> </th>
<th>YES</th>
<th>NO</th>
</tr>
</thead>
<tbody style="">
<tr style="">
<td>1. Do you require liability coverage in your lease at your properties?</td>
<td class="count-me">0</td>
<td class="count-me">75</td>
</tr>
<tr style="">
<td style="">2. If yes, do you have a method of ensuring residents maintain liability coverage throughout the course of their lease?</td>
<td class="count-me">0</td>
<td class="count-me">75</td>
</tr>
<tr style="">
<td>3. If yes, do you have a preferred provider for renters insurance and liability coverage?</td>
<td class="count-me">0</td>
<td class="count-me">10</td>
</tr>
<tr style="">
<td>4. If yes, does your management team and property staff understand what your preferred partner’s program covers?</td>
<td class="count-me">0</td>
<td class="count-me">10</td>
</tr>
<tr style="">
<td>5. Since you or your preferred partner implemented liability coverage requirements, have you had an uninsured loss caused by a resident that resulted in fire, smoke, explosion or water damage to the property?</td>
<td class="count-me">30</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td style="">6. Do residents have easy access to purchase an insurance policy from your preferred partner via website, mobile, phone and/or point of lease enrollment?</td>
<td class="count-me">0</td>
<td class="count-me">5</td>
</tr>
<tr>
<td>7. Do the majority of residents choose your preferred partner’s policies?</td>
<td class="count-me">0</td>
<td class="count-me">5</td>
</tr>
<tr style="">
<td>8. Do you feel your site staff spends too much time managing or following up with residents to ensure they meet their lease requirements for liability coverage?</td>
<td class="count-me">5</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td style="">9. Do you believe you wrote off too much bad debt last year?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
<tr style="">
<td>10. Do you believe that your collections company should be recovering more from delinquent residents?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
<tr>
<td>11. Do you feel that you should get better support, have better preferred partner take rates, more ancillary revenue, and have less site staff workload with your renters insurance program?</td>
<td class="count-me">35</td>
<td class="count-me">0</td>
</tr>
<!-- add the total points row here -->
<tr>
<td>
<div class="section-content">YOUR TOTAL POINTS <span class="sum">0</span></div>
</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
<br/>
<span id="spnText"></span>
也在 CodePen.