Need to take a SELECT drop down list options and find if any of the values are whole numbers, then append a .00 to the list value option. need to change the value as well.
<select>
<option value="1">1</option>
<option value="1.99">1.99</option>
<option value="2.99">2.99</option>
<option value="4">4</option>
</select>
Thanks in advance for any help on this
Need to take a SELECT drop down list options and find if any of the values are whole numbers, then append a .00 to the list value option. need to change the value as well.
<select>
<option value="1">1</option>
<option value="1.99">1.99</option>
<option value="2.99">2.99</option>
<option value="4">4</option>
</select>
Thanks in advance for any help on this
Share Improve this question edited May 28, 2009 at 20:43 Paolo Bergantino 489k82 gold badges521 silver badges437 bronze badges asked May 28, 2009 at 20:21 Phill PaffordPhill Pafford 85.4k92 gold badges266 silver badges384 bronze badges 4- This really isn't much of JQuery question, its just standard javascript. JQuery can be used for easily extracting the values from the select box, but it has nothing to do with the calculation. – Soviut Commented May 28, 2009 at 20:28
- Not really sure what you were doing, but the 2nd solution doesn't round up the numbers... at least not with the data you have up there... – Paolo Bergantino Commented May 29, 2009 at 18:07
- I just need to append the .00 if the number is a whole number 0-9 no need to round the number either. Thanks! – Phill Pafford Commented May 29, 2009 at 18:13
- That is what my example is doing, though. I'm not sure where you got that it rounds the numbers. Did you click on the demo link I posted? jsbin./ayumi - it keeps 1.99 and 2.99 as is, but adds .00 to 1 and 4. isn't that what you want? – Paolo Bergantino Commented May 29, 2009 at 22:11
2 Answers
Reset to default 6Not quite sure if this is the best way, but it works:
$('option', '#myselect').each(function() {
if($(this).val() == parseInt($(this).val(), 10)) {
var x = $(this).val() + '.00';
$(this).val(x).text(x);
}
});
Demo.
On second thought, you could also do this using toFixed
, which is cleaner:
$('option', '#myselect').each(function() {
var x = Number($(this).val()).toFixed(2);
$(this).val(x).text(x);
});
Demo.
If you plan on using string formatting a lot, i remend getting the plugin:
http://code.google./p/jquery-utils/wiki/StringFormat
It would then be as easy as: $.format('{a:.2f}', {a:'1'})
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742340439a4425517.html
评论列表(0条)