assign the value to a variable in the onchange event of the select

0

Well my problem is this:

Since a while ago I have been trying to modify the select that appears in a table, this modifies the row where the select was changed but since the data is displayed by means of a foreach it only works with the first element but when changing the second element it does not do anything

My first question is:

What do you recommend me to do?

and if there are no recommendations my question would be

How do I have the select onchange event send a function and when I call it send send the value of the select and the value of another field? for example:

<select> onchange"(funcion(valor del select, id))"</select>
    
asked by Obeth Morales 17.08.2018 в 18:11
source

3 answers

1

You can try the following: in the onchange with this.value you pass the value of the select and with the echo you pass the value of your php variable to the function (assuming you have everything in the same php file)

php:

<?php
$otraVariable = 'hello';
?>

html (within the same php):

<select name="anyName" onchange="myFunction(this.value, '<?php echo $otraVariable;?>')">
    <option>Selecciona una opción</option>
    <option value="1">Valor 1</option>
    <option value="2">Valor 2</option>
    <option value="3">Valor 3</option>
</select>

js:

function myFunction(opSelect, otraVar) {
    alert(opSelect + ' ' + otraVar);
}
    
answered by 17.08.2018 / 18:20
source
1

Look at this example is very small

$(document).ready(function(){
    
       $("#select").on("change",function(){
    var otro=$("#otro").val();   
        var valorSelect=$(this).val()//obtenemos el valor seleccionado en una variable
      console.log( valorSelect+otro)
      })
    
    })
<input type="hidden" value="50" id="otro"><!--otro input tipo hidden con un valor-->
    <select name="anyName" id="select">
            <option value="1">Valor 1</option>
            <option value="2">Valor 2</option>
            <option value="3">Valor 3</option>
        </select>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
answered by 17.08.2018 в 18:24
0

I did not really understand what you need, but that way you can capture the text and the value of a select, and select another input and get the value:

function funcion(e) {
  var option = e.options[e.selectedIndex];
  var inputAux = document.getElementById('inputAux');
  alert("Valor Option: " + option.value + ", Texto Option: " + option.text);
  alert("Valor Input: " + inputAux.value);
}
<select onchange="funcion(this);">
  <option value="0">Item 1</option>
  <option value="1">Item 2</option>
  <option value="2">Item 3</option>
</select>
<input type="text" id="inputAux" value="3">
    
answered by 17.08.2018 в 18:33