Get the value of the selected item in a Classic Sp Combobox

Asked

Viewed 38,900 times

4

I have two combobox, I need to get the id of one to load the other from the item that was selected in the combobox1,how could I do this through a javascript function.

<select id="cb_catinsumo" class="combo" name="cb_catinsumo" class="combo2">
    <option value="">Selecione uma categoria</option>
    <%monta combo de categoria de insumos sSQL="SELECT categoria_insumo FROM tb_catinsumo ORDER BY categoria_insumo" oQueryCatInsumo.Open sSQL, oConexao IF NOT oQueryCatInsumo.EOF THEN oQueryCatInsumo.MoveFirst DO until oQueryCatInsumo.EOF%>
        <option value="<%=oQueryCatInsumo(" categoria_insumo ").value %>">
            <%=oQueryCatInsumo( "categoria_insumo").value %>
        </option>
        <% oQueryCatInsumo.MoveNext LOOP ELSE %>//sem categorias cadastradas
            <option value="">NÃO HÁ CATEGORIAS CADASTRADAS</option>
            <% END IF %>
</select>
  • Can you put your HTML? otherwise we will reply to imagine your code.

  • @Sergio, my code is the one where you fill the first combo,,

  • Everton, no problem. You can put the code, then select all and do CTR+K, to make a TAB to the whole code.

  • Got it @Sergio, through this code would have some idea how to help me?

  • Everton, ASP is not my strong suit, but this select is exported correctly, right? and you want to call another select according to its choice. Then you need to use ajax. Give a few minutes that surely someone safer than me in ASP will help more. The important thing is that you explain the question as best you can.

  • Yes it is exported correctly, ok thanks for helping me up to this point.

Show 1 more comment

1 answer

17


When your combobox is mounted it will look like the following structure:

<select id="cb_catinsumo">
  <option value="1">item1</option>
  <option value="2" selected="selected">item2</option> /*atente que aqui o item está selecionado*/
  <option value="3">item3</option>
</select>

Below the javascript code to capture VALUE and TEXT:

1) RECOVERS VALUE OF SELECTED ITEM

var e = document.getElementById("cb_catinsumo");
var itemSelecionado = e.options[e.selectedIndex].value;

2) Retrieves text from selected item

var e = document.getElementById("cb_catinsumo");
var itemSelecionado = e.options[e.selectedIndex].text;

There are other ways to capture the values, but this believe that fully meets your need, has faster forms, for example with jQuery, if you do not know of a researched on because it is much more productive in some situations.

  • Thanks for the help!

Browser other questions tagged

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