1
I have a table of plans here, and in it I had 5 columns, for 5 planes obviously.
In each column there is a select to choose the periodicity of each plan.
According to the option selected in this select, it must change the value that is being displayed for the plan.
I tried to start a script but couldn’t get it to work as needed.
I’ll leave the full code below:
JAVASCRIPT:
<script>
$(function() {
// PLANO BÁSICO US
// preciso usar o str_replace por que o valor é obtido através de uma variável PHP
var plano_basicoUS = {
1: <? echo str_replace(',', '.', 11,90); ?>,
2: <? echo str_replace(',', '.', 32,70); ?>,
3: <? echo str_replace(',', '.', 59,40); ?>,
4: <? echo str_replace(',', '.', 106,80); ?>,
};
$("#periodicidadeBasicoUS").on('change', function() {
var periodo = this;
var escolha = $(this).val();
var multiplicar = $(this).find(':selected').data('multiplicar');
$.each($('.periodo-item'), function(key, value) {
var plano = $(value).data('plano');
var preco = plano_basicoUS[1] * multiplicar;
$(value).find('.valor_original').text('R$ '+preco);
$(value).find('.valor_desconto').text('R$ '+plano_basicoUS[2]);
$(value).find('.valor_mensal').text(plano_basicoUS[1]);
});
});
$("#periodicidadeBasicoUS").trigger('change');
});
</script>
HTML:
<br>
<div class="periodo-item" data-plano="1">
<span class="valor_original"></span> <span class="valor_desconto"></span>
<br>
<span class="valor_mensal"></span>
</div>
<div class="select">
<select name="periodicidadeBasicoUS" id="periodicidadeBasicoUS">
<option value="1">Mensal - Sem desconto</option>
<option data-multiplicar="3" value="2">Trimestral - </option>
<option data-multiplicar="6" value="3">Semestral - </option>
<option data-multiplicar="12" value="4">Anual - </option>
</select>
</div>
I need him to do the following:
- Return the values with comma instead of point, ex: R$ 11,90.
- If selected the option with value 1 (monthly) it must show any text in
span
classy: original value and value_discount and to the class monthly value would be the value ofplano_basicoUS[1]
, for the other options the normal numerical values will be displayed.
How should I adjust the script?
Since there are several plans, if anyone knows how to simplify this code would be interesting, as I will need to repeat all the code for the next plan.
Note that you are not saving the data you are pulling from PHP in an array, but in a javascript object. That’s probably why it’s not printing when you use it
plano_basicoUS[1]
– João Pedro Henrique
He’s printing, only he’s showing it with a dot and no zero at the end.
– Wendler
Yes, I realized only after you had the correct object indicies... Is the data type entering str_replace really a string? Note that without the quotes, you are passing a number. Also, the order of the parameters is changed. It should be
echo str_replace('.', ',', "11.90");
https://ideone.com/JqB4FD– João Pedro Henrique
So, in PHP it returns the value with comma, then as JS does not do the multiplication with comma I needed to use str_replace to convert with dot.
$basicoUS_mensal > 11,90
– Wendler
I understand. Now come on. Do you get the PHP string already formatted and want to do the operation? You would have to use parseFloat in this case. You cannot multiply a string by a number for example. It would look something like this:
parseFloat(<?echo str_replace(',', '.', "11,90");?>)
. That’ll give you the real number11.9
in JS and will allow you to perform operations with it. Then, to print you would need to format to the way you want the final value to be printed.– João Pedro Henrique
Yes, I get the string already formatted with comma. I think it was you who helped me in the other question right? https://answall.com/questions/306283/motrar-valor-de-file-get-contents-mas-somente-o-que-esta-entre-aspas/306309#306309
– Wendler
Right, I would suggest you, if possible, to change the comma to a point, if possible, already in the PHP file that returns this data to you, and in javascript just use
parseFloat()
so you can do math operations using this value and then format it so you can print it as you want.– João Pedro Henrique
And how can I format the value that is printed by js?
– Wendler
Then you would have to create a function that formats that number. Example: https://ideone.com/vEFRoQ
– João Pedro Henrique