简体   繁体   中英

How to display a div when values are selected from TWO dropdown?

Here I have two dropdown

<div class="form-group">
    <label class="col-sm-4 control-label">Car Type</label>
    <div class="col-sm-7">
        <select class="form-control" id="cartype" name="cartype">
           <option value=""> -- Select Type -- </option>
           <option value="Innova">Innova</option>
           <option value="Tata">Tata</option>
           <option value="Mahindra">Mahindra</option>
        </select>
    </div>
</div>

<div class="form-group">
    <label class="col-sm-4 control-label">Rate Type</label>
    <div class="col-sm-7">
        <select class="form-control" id="rate" name="rate" onchange="showdiv()" >
            <option value=""> -- Select Type -- </option>
            <option value="Outstation" name="outstation" id="outstation">Outstation</option>
            <option value="Local" name="local" id="local">Local</option>
        </select>
    </div>

And a Text Box

<div class="form-group" id="showme" style="display:none;">
    <input type="text" id="txt1" name="txt1">
</div>

My Javascript

function showdiv(){
  var vehicle = document.getElementById("cartype").value = "Innova || Tata";
  var rate = document.getElementById("rate").value = "Local";   
  document.getElementById("showme").style.display = 'block';
}

Here I need to display the Text Box div only when cartype is equal to Local . But when I select Rate Type the hidden div is displaying.

Can you help me out where is the error in my code??

this should be working:

function showdiv(){
  var vehicle = document.getElementById("cartype").value;
  var rate = document.getElementById("rate").value;

  if ((vehicle == "Innova" || vehicle == "Tata") && rate == 'Local') {
     document.getElementById("showme").style.display = 'block';
  } else {
     document.getElementById("showme").style.display = 'none';
  }
}

Try this

 function showdiv(){
       if((document.getElementById("cartype").value == "Innova" || document.getElementById("cartype").value == "Tata") &&  (document.getElementById("rate").value == "Local")){
                document.getElementById("showme").style.display = 'block';
       } else {
       document.getElementById("showme").style.display = 'none';
       }


}

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