[英]Couting how many times each option is selected in Select drop down
I am working on a form to help someone do classroom observations. 我正在设计一种表格,以帮助某人进行课堂观察。 The form has three main goals. 该表格有三个主要目标。
In case it makes a difference there a total of 144 intervals. 如果有所不同,则总共有144个间隔。
Here is a sample of my form and the script that I have working that solved my need #2. 这是我的表单示例以及可以解决我的需求#2的脚本。
My script 我的剧本
$('select[id*="interval"]').change(function() {
total = 0;
$('select[id*="interval"]').each(function() {
total += (+this.value);
});
document.getElementById('result').innerHTML = total;
});
Bits of my form 我的形式
Where the script results print 脚本结果的打印位置
<td><label>Total # Of Observation Intervals:</label></td><td><span id="result">0</span></td>
Where I want the options to be totaled 我希望将选项汇总的位置
<tr>
<td><label>1. Lying Down:</label></td><td><span id="resultA">0</span></td><td>%</td>
<td><label>2. Sitting:</label></td><td><span id="resultB">0</span></td><td>%</td>
</tr>
<tr>
<td><label>3. Standing:</label></td><td><span id="resultC">0</span></td><td>%</td>
<td><label>4. Walking:</label></td><td><span id="resultD">0</span></td><td>%</td>
</tr>
<tr>
<td><label>5. Very Active:</label></td><td><span id="resultE">0</span></td><td>%</td>
</tr>
A couple of my dropdowns 我的几个下拉菜单
<td><label class=select>Interval 1</label></td><td>Student Activity</td><td><select data-mini=true id="interval1">
<option value=0></option>
<option value=1 id="optA">1</option>
<option value=1 id="optB">2</option>
<option value=1 id="optC">3</option>
<option value=1 id="optD">4</option>
<option value=1 id="optE">5</option>
<option value=1 id="optF">6</option>
</select>
</td>
</td>
</tr>
<tr>
<td><label for=select-choice-4 class=select>Interval 2</label></td><td>Student Activity</td><td><select data-mini=true id="interval2">
<option value=0></option>
<option value=1>1</option>
<option value=1>2</option>
<option value=1>3</option>
<option value=1>4</option>
<option value=1>5</option>
<option value=1>6</option>
</select>
</td>
Something I tried that did not work 我尝试过的某事不起作用
$('select[id*="interval"]').options[1].value.change(function() {
totalA = 0;
$('select[id*="interval"]').options[1].value.each(function() {
totalA += (+this.value);
});
document.getElementById('resultA').innerHTML = totalA;
});
Firstly: Is the total variable defined somewhere outside the change(function(){}) scope? 首先:总变量是否在change(function(){})范围之外的某个地方定义? It should be. 它应该是。
Secondly: what your code does is - whenever each select changes, the first thing that it does is setting the total to 0 - no wonder why the total is wrong. 其次:您的代码要做的是-每当每个选择更改时,它所做的第一件事就是将合计设置为0-难怪为什么合计是错误的。
Thirdly: make sure you understand what $().each and $().change do, cause the whole program flow seems to be flawed 第三:确保您了解$()。each和$()。change的作用,导致整个程序流程似乎有缺陷
Moreover: you shouldn't have a similar value repeated several times in the same select 此外:同一选择中不应多次重复相似的值
I can't understand what's your goal exactly, but this code here accomplishes something similar: 我无法确切地理解您的目标是什么,但是这里的代码完成了类似的工作:
var totals = { //totals
interval1: {
optionA: 0,
//...
optionZ: 0
},
interval2: {
optionA: 0, //total hits for: <select id="interval2"> <option value="optionA">
optionB: 0,
optionC: 0
}
//etc...
};
$('select[id*="interval"]').each(function(){
$(this).change(function(){ //attach onchange event to each of the selects
var $s = $(this);
if(totals[$s.attr('id')]){
totals[$s.attr('id')][$s.val()] += 1;
//update the span corresponding to the particular select and the particular option value:
$('#result_for_' + $s.attr('id') + '_' + $s.val()).text(totals[$s.attr('id')][$s.val()]);
}
})
});
This is a solution that bases on unique value of each option (per select, they can repeat across selects) 此解决方案基于每个选项的唯一值 (每个选择,它们可以在选择中重复)
This solution is also not optimal - you shouldn't search the DOM for $('#result_for_' + ....) everytime you want to update it. 此解决方案也不是最佳方案-每次要更新DOM时都不应在DOM中搜索$('#result_for_'+ ....)。 You should remember all the counters instead. 您应该记住所有计数器。
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.