How do I write an if statement so that it applies only when Value A changes to Value B in a select box? http://ift.tt/1Sa36v7
I have the following select box for "miles" and input box for "residual":
<div class="item_div"><label>Miles:</label>
<select id="miles">
<option value="10000">10,000</option>
<option value="12000" selected>12,000</option>
<option value="15000">15,000</option>
</select>
</div>
<div class="item_div">
<label>Residual: </label>
<input type="number" id="resP" class="rmVal" value="52"> %
</div>
<div>
<label>Result (10000*Residual)</label>
<input id = "result"></input>
</div>
I would like to add 1 to the miles <input>
value if the user changes from 12000 to 10000. If the user changes from 10000 to 15000, I would like to subtract 3. If the user then changes from 15000 to 120000, I would like to +1 and so on.
The residual values for corresponding miles are:
10000 miles: 53%
12000 miles: 52%
15000 miles: 50%
The reason I wanted to make the residual change based on the miles change is that later on, I want the user to be able to manually change the residual value and adjust the residual value based on the miles.
I tried to following code to +1 when the $("#miles")
changes from 12000 to 10000 and -2 if it changes from 12000 to 15000 (line 13-26):
if (miles.val() == "12000") {
miles.change(function() {
if (miles.val() == "10000") {
resN = resN +.01;
resP.val(parseFloat(resP.val())+1);
calculate();
}
else if (miles.val() == "15000") {
resN = resN -.02;
resP.val(parseFloat(resP.val())-2);
calculate();
}
})
};
I repeated the if statement for the other miles changes (line 27-55). However, the code only works correctly for the 1st and 2nd miles change. The 3rd time on is messed up. I was wondering how I may correct my code and whether there is an easier way to accomplish this. Thank you very much! I much appreciate it!
Aucun commentaire:
Enregistrer un commentaire