Multiply value by taking only the first character

1

Personal I need an aid because I am not aware of javascript

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

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

But this is how I need the code to accept the calculation.

  • value="2-cd" ... I need javascript to perform the multiplication just for the first character

I need an alternative that does not modify this code very much.

Below is 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:<inputtype="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"     />
    
asked by anonymous 14.09.2017 / 06:09

1 answer

1

To return the first digit, use the substring() method that returns a subset of a string from one index to another, or to 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 ()

    
14.09.2017 / 15:07