检查选项是否显示为其他选项值

时间:2015-10-21 12:23:11

标签: javascript jquery

我有类似的东西:

<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>

我想这样做,如果在#BadReason中选择了Scrammer,那么在#BanLength中自动选择1周。

2 个答案:

答案 0 :(得分:0)

尝试使用change event BanReason并根据此值将所需值传递给BanLength并触发更改。:

&#13;
&#13;
     $("#BanReason").on("change",function(){
               if(this.value == "scrammer"){
            	   $("#BanLength").val("1week").trigger("change");
               }
       });
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

关于更改事件将帮助您执行此操作

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

为此添加了一个小提琴 Fiddle