Store select result

0

In a previous post ask this: Download selected object and thanks to a user gave me the idea, which I appreciate very much. But now I have another doubt, and since I have to have 50 of respect to be able to respond and others, well I make another post.

And the question now is: Is there any way to store the result of a select? That is, store that has been selected in the "select" and then save it in a variable and use it.

Greetings and thank you very much.

    
asked by Straxy 24.05.2017 в 11:20
source

1 answer

1

I show you a simple example of HTML combined with jQuery. The jQuery library is added in the HTML using the line: <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

You'll see that then jQuery listens for the event on change of select by launching an action each time you change the option.

In this part of the code:

$('select').on('change', function() {... You can launch any type of action: requests to the server, query of database, consult external APIs ... and present your data on the same page. You will see that when you select an option the content of the div is changed dynamically, without having to change the page.

To test, press the blue button Run .

$('select').on('change', function() {
  textohtml = '<p>Has seleccionado el valor: <b>' + this.value + '</b><br />'+
  'Desde aquí puedes enviar peticiones al servidor. Por ejemplo, consultar la BD usando el id cuyo valor es el seleccionado, en este caso: ' + this.value+
  '</p><p>Cambia a otro valor y verás <b>como el contenido se actualiza de forma dinámica</b> :)</p>' ;
  $('#result').html(textohtml);
})
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<select>
    <option value="0">-Seleccione Fecha-</option>
    <option value="1">1. 2017-05-01</option>
    <option value="2">2. 2017-05-02</option>
    <option value="3">3. 2017-05-03</option>
</select>
<div id="result"></div>

Note: It's a basic example, and as it says @ G3l0 in your comment, you can listen to any element of the html document by type of element, as in this example, in which you would hear the on change of any select , but you can listen to your particular elements, using the id of them, you can also listen to the name of the class, or by groups of elements, as is usually the case with radio buttons for multiple selections, etc., etc.

The options are many. And also say that you can update from jQuery anything in your current HTML document: input values, content of elements <div>, <p>, <span> , etc, colors of text or background, applying CSS rules from the same jQuery ... in short, everything a range of possibilities :) ...

    
answered by 24.05.2017 / 12:41
source