Hello.

I have two inputs where value can be 1 233 323 and 14 987 322 (prices in millions). The task of rounding and displaying only the bits of the millin 1 million and 14 million in this case.

I write this:

HTML:

<p> <label for="mse2_ms|price_0"> <span>От</span> <input id="mse2_ms|price_0" type="input" value="2673990" name="ms|price"> </label> <label for="mse2_ms|price_1"> <span>До</span> <input id="mse2_ms|price_1" type="input" value="3483545" name="ms|price"> </label> <span>Млн. руб.</span> </p> 

Jquery:

 $('.mfilter-price').val($('.mfilter-price input').val().substring(0, 1)); 

In this case, with the given html in all input it is written 2.
I can not understand how for each unique value to change.

Thank you in advance.

    2 answers 2

    jQuery.each() to apply the jQuery.each() to all captured elements:

     $( 'input[name="ms|price"]').each( function ( i, el) { $( el).val( Math.floor( // округляем до целого, не превосходящего значение // возможно, стоит округлять до ближайшего: Math.round() parseInt( // парсим значение как целое число $(el).val().replace( /\s+/g, "") // убираем пробелы из значения, если есть ) / 1E6 // нужны миллионы – делим на миллион ) ); }); 

    Working example

    Ps did not write anything new, just issued a separate answer.

    • Thank you very much. Everything works like a Swiss watch. Do not count for arrogance, but how to constantly monitor input `s? The fact is that under them I also have a slider slider, where these intervals change and when they are dragged, prices return to millions to the nearest ruble (by the way, the gaps - I just put the question in order to make it visually clearer) - Romancho
    • The slider probably telegraphs its motion events - find its documentation. It is more logical to listen to its events, and update the value of them; rather than listening to changes in values. - Sergiks

    How do you like this option:

     '14 434 434'.split(' ').length === 3 ? '14 434 434'.split(' ')[0]:false 

    Try it in the console :) Well, seriously:

     function roundToM(t){ var _t = t.split(' '); if (_t===3) { return _t[0]; } else { return false; } } 
    • @Oleg B, you forgot in the condition _t.length and value values, I watch without spaces. If this is the case, then you can change your code in such a way: function roundToM (t) {var parts = t.split (/ (? = (?: \ D {3}) * $) /); if (parts.length == 3) {return parts [0]; } else {return false; }} - Deonis
    • The author seems to write где value могут быть 1 233 323 и 14 987 322 , if value were without a space, you can then simply divide by a million, and use round , something like Math.round(14355600/1000000) - ferrari
    • @ Oleg B, The author seems to write ... and so, and so: in the description with spaces, in the code - without. Therefore, let the author himself decide. And the second, for example: console.log (Math.round (14555600/1000000)); // 15, not 14 - Deonis
    • then Math.floor (); :) - ferrari
    • @ Oleg B, this is closer to the truth ツ - Deonis