Multiply value by taking only the first character

Asked

Viewed 69 times

0

Personal I need a help because I have no knowledge of javascript

In this script below it makes calculations according to a chosen field It works perfect for me, but my problem is in the dropdown. this dropdown it does a multiplication by value.

  • value = "2" ... ok works perfect

But that’s how I need the code to accept calculation

  • value = "2-cd" ... I need javascript to do the multiplication so only by the first character

I need an alternative that doesn’t change that code too much.

Below follows my code as an example

$(document).ready(function() {
  $(".valores1").change(function() {
    var total = 1000;
    total += $('input[class="valores1"]:checked').get().reduce(function(tot, el) {
      return tot + Number(el.value);
    }, 0);
        
        
    var e = document.getElementById("valores2");
    var itemSelecionado = e.options[e.selectedIndex].value;
    total=total+(itemSelecionado*20);
    $('#total1').val(total.toFixed(2));        
  });  
});
</script>
 

<script>
  function Mudarestado(el) {
        var display = document.getElementById(el).style.display;
        if(display == "block")
            document.getElementById(el).style.display = 'none';
        else
            document.getElementById(el).style.display = 'block';
    }
      
   
					
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
Duplica:<input type="checkbox" class="valores1" onclick="Mudarestado('minhaDiv')" name="direcao" value="1000" id="option_1"  />
Adciona:<input type="checkbox" class="valores1" name="bilingue" value="200" id="option_1"  />
<select id="valores2"   class="valores1 form-control" name="cadeirinha" >
    <option value="0">0</option>
    <option value="2">Um Bebê Conforto</option>
    <option value="2-bc2">Dois Bebê Confortos</option>
    <option value="1-cd1">Uma Cadeirinha</option>
    <option value="2-cd2">Duas Cadeirinhas</option>
    <option value="1-as1">Uma Assento de elevação</option>
    <option value="2-as2">Dois Assento de elevação</option>
    <option value="2-ascd">Um Assento de elevação e Uma Cadeirinha</option>
    <option value="2-bccd">Um Bebe Conforto e Uma Cadeirinha</option>
    <option value="2-asbc">Um Assento de elevação e Um Bebe Conforto</option>
</select>  

<input type="text" size="5"  readonly="" name="valor" id="total1" value="1000.00"     />

  • But will it multiply by what value ? Imagining that you choose Duas cadeirinhas multiplies 2 for how much ? And if it is Dois Assento de elevação ? Will multiply 2 by how much ? The structure you are using seems confusing to me and leads to errors

1 answer

1


To return the first digit use the method substring() that returns a subset of a string between an Indice and another, or until the end of the string.

In your case itemSelecionado.substring(0,1)

    var e = document.getElementById("valores2");
    var itemSelecionado = e.options[e.selectedIndex].value;
    //aqui pega primeiro digito
    var primeiroDigito = (itemSelecionado.substring(0,1));
    total=total+(primeiroDigito*20);
    $('#total1').val(total.toFixed(2));

Source - substring()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.