admin管理员组文章数量:1332718
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'})
本文标签: javascriptjQuery find whole number and append decimalStack Overflow
版权声明:本文标题:javascript - jQuery find whole number and append decimal - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742340438a2456493.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论