[英]Option variables handling with jQuery in select change function
I tried to write some jQuery functions to change some variables values when options of a select item are selected (add a certain value if option is selected, subtract that value if option is not selected anymore), but it adds and subtracts values in a very odd way. 我试图编写一些jQuery函数,以在选择选择项的选项时更改一些变量值(如果选择了选项,则添加一个特定值,如果不再选择选项,则减去该值),但是它在一个非常非常的值中添加和减去值奇怪的方式。 Here's the code I wrote for the javascript (adapted from a previous code that used to work with checkboxes): 这是我为javascript写的代码(改编自以前用于复选框的先前代码):
var pds1=0;
var pds2=0;
var pds3=0;
var pdstot=0;
$(document).ready(function() {
$('#perk1').html(pds1);
$('#perk2').html(pds2);
$('#perk3').html(pds3);
$('#perktot').html(pdstot);
$("#blabla").change(
function() {
if ($("option#5:selected").length)
{
pds1=pds1+10;
pdstot=pdstot+10;
}
else if(pds1>0)
{
pds1=pds1-10;
pdstot=pdstot-10;
}
if ($("option#3:selected").length)
{
pds2=pds2+10;
pdstot=pdstot+10;
}
else if(pds2>0)
{
pds2=pds2-10;
pdstot=pdstot-10;
}
if ($("option#4:selected").length)
{
pds3=pds3+10;
pdstot=pdstot+10;
}
else if(pds3>0)
{
pds3=pds3-10;
pdstot=pdstot-10;
}
//option 6 adds 10 to variable pds1 and pds2
if ($("option#6:selected").length)
{
pds1=pds1+10;
pds2=pds2+10;
pdstot=pdstot+10;
}
else if(pds1>0)
{
pds1=pds1-10;
pds2=pds2-10;
pdstot=pdstot+10;
}
$('#perk1').html(pds1);
$('#perk2').html(pds2);
$('#perk3').html(pds3);
$('#perktot').html(pdstot);
});
});
and here's the html 这是HTML
<select id='blabla' multiple='multiple'>
<option id='5'>Name5</option>
<option id='3'>Name3</option>
<option id='4'>Name4</option>
<option id='6'>Name6</option>
</select>
<span id="perk1"></span>
<span id="perk2"></span>
<span id="perk3"></span>
<span id="perk4"></span>
Can someone explain me how this function is handling variables? 有人可以解释一下此函数如何处理变量吗? Odd behavior example: clicking option 5 doesn't add 10 to pds1, but instead it subtracts 10 to pds2; 奇怪的行为示例:单击选项5不会将10加到pds1,而是将10减到pds2。 what I'd like to happen instead it's to add 10 to pds1 and do nothing to pds2. 我想发生的事情是在pds1中添加10,而对pds2不执行任何操作。 I think the problem, in this case, it's in the "else if" under "option#6:selected", but why? 我认为问题出在这种情况下,在“ option#6:selected”下的“ else if”中,但是为什么呢? And it's not the only odd behavior. 这不是唯一的奇怪行为。
It's easy to see why you get the results you are getting. 很容易看出为什么获得结果。 Using current code and the #5 example walk through each of the if
statements. 使用当前代码和#5示例遍历每个if
语句。 Since #5 is selected pds1
gets 10 added. 由于选择了#5,因此pds1
将添加10。 Since #6 isn't selected and pds1 got increased by #5 the else
subtracts what was just added to pds1. 由于未选择#6,并且pds1增加了#5,因此else
减去刚添加到pds1的内容。
You can simplify this whole logic by simply resetting the variables to zero each time, and then only adding what is needed. 您可以通过简单地每次将变量重置为零,然后仅添加所需的内容来简化整个逻辑。
/* utilty function to avoid repeated lines */
function showValues(pds1, pds2, pds3) {
var pdstot = pds1 + pds2 + pds3
$('#perk1').html(pds1);
$('#perk2').html(pds2);
$('#perk3').html(pds3);
$('#perk4').html(pdstot);
}
$(document).ready(function() {
showValues(0, 0, 0);
$("#blabla").change(
function() {
var num_selected = $(this).find(':selected').length;
var pds1 = 0;
var pds2 = 0;
var pds3 = 0;
if ($("option#5:selected").length) {
pds1 = pds1 + 10;
}
if ($("option#3:selected").length) {
pds2 = pds2 + 10;
}
if ($("option#4:selected").length) {
pds3 = pds3 + 10;
}
//option 6 adds 10 to variable pds1 and pds2
if ($("option#6:selected").length) {
pds1 = pds1 + 10;
pds2 = pds2 + 10;
}
showValues(pds1, pds2, pds3);
});
});
DEMO: http://jsfiddle.net/f9UEC/ 演示: http : //jsfiddle.net/f9UEC/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.