admin管理员组文章数量:1355675
I'm currently using a range slider for price ranges of £0 to £2,000,000. Currently, everything is working correctly apart from the prices not being formatted as currency (i.e the£500,000 value is currently being shown as £500000 - without the mas). Here's the code:
jQuery
/* SALES SLIDER */
$( "#slider-range-price-sales" ).slider({
range: true,
min: 0,
max: 2000000,
step: 50000,
values: [ 0, 2000000],
slide: function( event, ui ) {
$("#minPrice").val("£" + ui.values[0]).toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "£1");
$("#maxPrice").val("£" + ui.values[1]);
}
});
$("#minPrice").val("£" + $("#slider-range-price-sales").slider("values", 0));
$("#maxPrice").val("£" + $("#slider-range-price-sales").slider("values", 1));
HTML
<div id="slider-range-price-sales"></div>
<div class="priceContainer">
<div class="left"><input type="text" id="minPrice" name="minPrice" style="border: 0; color: #3D80E9; background:#F2F2F2; font-weight: bold;" /></div>
<div class="right"><input type="text" id="maxPrice" name="maxPrice" style="border: 0; color: #3D80E9; background:#F2F2F2; font-weight: bold;" /></div>
</div>
/
I've tried reformatting the minimum price with JavaScript .replace to no avail.. any help or suggestions would be greatly appreciated.
Thanks, jamie
I'm currently using a range slider for price ranges of £0 to £2,000,000. Currently, everything is working correctly apart from the prices not being formatted as currency (i.e the£500,000 value is currently being shown as £500000 - without the mas). Here's the code:
jQuery
/* SALES SLIDER */
$( "#slider-range-price-sales" ).slider({
range: true,
min: 0,
max: 2000000,
step: 50000,
values: [ 0, 2000000],
slide: function( event, ui ) {
$("#minPrice").val("£" + ui.values[0]).toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "£1");
$("#maxPrice").val("£" + ui.values[1]);
}
});
$("#minPrice").val("£" + $("#slider-range-price-sales").slider("values", 0));
$("#maxPrice").val("£" + $("#slider-range-price-sales").slider("values", 1));
HTML
<div id="slider-range-price-sales"></div>
<div class="priceContainer">
<div class="left"><input type="text" id="minPrice" name="minPrice" style="border: 0; color: #3D80E9; background:#F2F2F2; font-weight: bold;" /></div>
<div class="right"><input type="text" id="maxPrice" name="maxPrice" style="border: 0; color: #3D80E9; background:#F2F2F2; font-weight: bold;" /></div>
</div>
http://jsfiddle/isherwood/RwfFH/
I've tried reformatting the minimum price with JavaScript .replace to no avail.. any help or suggestions would be greatly appreciated.
Thanks, jamie
Share Improve this question edited Sep 24, 2013 at 21:18 isherwood 61.2k16 gold badges121 silver badges170 bronze badges asked Sep 24, 2013 at 21:06 Jamie MclaughlanJamie Mclaughlan 8752 gold badges10 silver badges30 bronze badges 3- @isherwood - why did you include a fiddle with bootstrap? – j08691 Commented Sep 24, 2013 at 21:17
- Oops. My mistake. jsfiddle/isherwood/RwfFH – isherwood Commented Sep 24, 2013 at 21:18
- This may be helpful. The fact that you're using a slider isn't really relevant. stackoverflow./questions/149055/… – isherwood Commented Sep 24, 2013 at 21:24
2 Answers
Reset to default 4To keep in line with what you are doing you just need to make a couple changes. I'll point out that you need to have modified the value before you set it in the .val() operation. Also, replace returns a new string it does not modify the original. That being done, you just needed a little tweak in the regex, $1, will use the matched value in the replace.
$("#minPrice").val("£" + ui.values[0].toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"));
You can use this function which add ma like you want
function addCommas(nStr){
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
and the jsFiddle : http://jsfiddle/RwfFH/1/.
本文标签: javascriptJquery UI Range SliderConvert number to currencyStack Overflow
版权声明:本文标题:javascript - Jquery UI Range Slider - Convert number to currency - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743956710a2568261.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论