How to get a data from a selected item in the combobox html

1

Good morning everyone. Galera is as follows, there are two questions to follow. I have 2 combobox! In the 1st combobox I have all the companies in the system. here everything is ok. In the 2nd combobox I have all the affiliations of the company selected by the 1st combobox. follows the function of the 2 combo box:

<script type="text/javascript">
        function uniopcao()
        {     
            $(document).ready(function() 
            { 
                var unid = $('#Unidades option:selected').val();
                var tensao = $('#Unidades option:selected').text();
                alert(tensao);
                $('#una').load('/site/unidade.php?uni='+ unid);
                $('#una').load('/site/unidade.php?ten='+ tensao);

            });
        }
        </script>

Notice that I need the value of the second combobox and also the text of the second combobox. and where the 1st problem arises ... In that part of code var tensao = $('#Unidades option:selected').text(); this text () returns the following:
andinthealertthatIasktodisplayIgetthefollowing:

Notice that in alert it shows me two values the "mt" and 429 .... now please understand how I can get only the value "mt" in the following code var tensao = $('#Unidades option:selected').text("eu queria o apenas o mt e não tudo mt-429");

If so, how? I would like to know please ...
already in the second doubt ... that I will only continue because it is related to the same question and the following in the second combobox there may be one or more affiliated affiliates ... as in the following image:

When you have only one branch it generates an error in return. which I do in the following select via php page follows the code:

<?php                       
session_start();
    include("conexao.php");           

            $unidade = filter_input(INPUT_GET, 'au', FILTER_VALIDATE_INT);
            $_SESSION['ulala'] = $_GET['au'];

            $comandoSql = "SELECT a,  b, c, Tensao
                            FROM Tab_UC where cod='".$ugauga."'" ;

            $dados = mysql_db_query($bancoDados, $comandoSql) or die (mysql_error());

            //$a='<option value="Selecione">Selecione sua Unidade: </option> ' ; 
            while ($linha = mysql_fetch_array($dados))
            {
               // $a.= '<option value="Selecione">Selecione sua Unidade: </option> ' ; 

                $a.='<option value= "'.$linha["a"].'">'.$linha["Tensao"].'-'.$linha["b"].'</option>';        
            }
            echo $a; 

?>

This error causes companies that only have a single branch, can not see a result of another select via another php page that does not come from the case .. and companies that have more than one affiliate I choose second affiliate and it shows correctly ... I need this line of code. / /% $a.= '<option value="Selecione">Selecione sua Unidade: </option> ' ; is presented in the 1st place of the combobox if they intented help me pf.

    
asked by anonymous 19.05.2016 / 16:14

1 answer

1

To get only the first two characters, use the following:

var tensao = $('#Unidades option:selected').text().substr(0, 2);
    
20.05.2016 / 03:08