How to take a data from a selected html combobox item

Asked

Viewed 1,164 times

1

Good morning, you guys. Guys, here’s the deal. I got two combobox! In the first combobox I have all the companies of the system. here everything ok. In the 2nd combobox I have all the affiliates 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>

Note that I need the value of the second combobox and also the text of the second combobox. and where the first problem arises... In that part of the code var tensao = $('#Unidades option:selected').text(); this text() returns me the following:inserir a descrição da imagem aqui
and in the Alert I ask to display me returns the seguite:inserir a descrição da imagem aqui

Notice that in Alert it shows me two values "mt" and 429.... now please intendam as I do to 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 you have 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 registered subsidiaries... as in the following image:inserir a descrição da imagem aqui

when only one affiliate generates an error in the return. I do in the following select via php page follow 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 makes that companies that only have a single affiliate, can not see a result of another select via another php page that is not there case.. and companies that have more than one affiliate I choose second affiliate and shows correctly... I need that line of code.//
$a.= '<option value="Selecione">Selecione sua Unidade: </option> ' ; is presented in 1st place of the combobox if you intend to help me pf.

  • @Diego, I don’t understand your edition...

  • @Victor Gomes the issue conflicted with his, I tried to devise the code, which was difficult to understand.

1 answer

1


To pick only the first two characters, use so:

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

  • exactly that!!!! congratulations very grateful

Browser other questions tagged

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