简体   繁体   中英

How to use a dropdown value as a if function

i have a html code as following

tfa_78 is a dropdown list, but build in formassembly, so not able to put it in the code the value "Test" is just to show myself, that the code in general is running ;)

<script>     

var tfa78 = document.getElementById("tfa_78").selvalue;

if( tfa78 == "karte" )
{
document.getElementById('tfa_2448').innerHTML ='<b>Bei karte anrufen.</b> <br>';
} else {
document.getElementById('tfa_2448').innerHTML ='Test';
}


my question: it is not working, i do not get the first result, what might be wrong? is the selvalue the right thing to select the dropdown value? before, i used this code with a textfield, that was working, so i guess it is anyhow related to the wrong "get" value argument?

Thanks a lot in advance for your support.

I updated my code already due to few comments as below:

 <script>     
 var tfa78 = document.getElementById("tfa_78").value;
 if( tfa78 == 'tfa_2438' ) {
 document.getElementById('tfa_2448').innerHTML ='<b> anrufen.</b> <br>';
 }else {
 document.getElementById('tfa_2448').innerHTML ='Test'+ tfa78;
 }</script>

and this is the dropdown code, but i can not change that.

<select id="tfa_78" name="tfa_78" title="Quelle?" class="required"><option 
value="">Bitte auswählen:</option>
<option value="tfa_2438" id="tfa_2438" data-conditionals="#tfa_93" 
class="">karte</option>
<option value="tfa_2437" id="tfa_2437" data-conditionals="#tfa_93" 
class="">Test</option>
</select>

To get the selected value you should use the property value . The selvalue I don't in which language exists. See following:

 function change(){ var tfa78 = document.getElementById("tfa_78").value; if( tfa78 == "karte" ) { document.getElementById('tfa_2448').innerHTML ='<b>Bei karte anrufen.</b> <br>'; } else { document.getElementById('tfa_2448').innerHTML ='Test'; } }
 <select id="tfa_78" onchange="change()"> <option/> <option value="karte">karte</option> <option value="diem">diem</option> </select> <div id="tfa_2448"/>

 <script>
    var elem = document.getElementById("tfa_78");
    var tfa78 = elem.options[elem.selectedIndex].value;
    if (tfa78 == "karte") {
        document.getElementById('tfa_2448').innerHTML = '<b>Bei karte anrufen.</b> <br>';
    } else {
        document.getElementById('tfa_2448').innerHTML = 'Test';
    }

    </script>

Use .value instead of .selvalue;

var tfa78 = document.getElementById("tfa_78").value;

Complete code is

<script>     
    function changeMessage() {
        var tfa78 = document.getElementById("tfa_78").value;
        console.log(tfa78);
        if( tfa78 == "volvo" ){
            document.getElementById('tfa_2448').innerHTML ='<b>Bei karte anrufen.</b> <br>';
        } else {
            document.getElementById('tfa_2448').innerHTML ='Test';
        }
    }

    </script>

    </head>

    <body>
    <select id="tfa_78" onchange="changeMessage()">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="fiat">Fiat</option>
      <option value="audi">Audi</option>
    </select>
    <div id = "tfa_2448"> </div>
    </body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM