简体   繁体   中英

Check if option is selected appear an other option value

I have something like :

<select id="BanReason">
<option value="hack">Hack</option>
<option value="badlang">Bad Language</option>
<option value="scrammer">Scrammer</option>
</select>

<select id="BanLength">
<option value="1day">1 Day</option>
<option value="2days">3 Days</option>
<option value="1week">1 Week</option>
</select>

I am trying to do, if Scrammer is selected in #BadReason select 1 week in #BanLength automaticly.

try using change event BanReason and based on this value pass the desired value to BanLength and trigger the change.:

  $("#BanReason").on("change",function(){ if(this.value == "scrammer"){ $("#BanLength").val("1week").trigger("change"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="BanReason"> <option value="hack">Hack</option> <option value="badlang">Bad Language</option> <option value="scrammer">Scrammer</option> </select> <select id="BanLength"> <option value="1day">1 Day</option> <option value="2days">3 Days</option> <option value="1week">1 Week</option> </select> 

On change event will help you to do this

$("#BanReason").on("change",function(){
    if($(this).val() == "scrammer")
        $("#BanLength").val("1week");
});

Added a Fiddle to this Fiddle

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