首先选择所有选项值 [英] Select all option values not first

查看:86
本文介绍了首先选择所有选项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在运行的脚本,该脚本根据选定的下拉选项显示/隐藏div.

I have a script that is working that show/hides divs based on the drop down option selected.

在用户在下拉列表中进行选择之前,我需要隐藏除第一个div以外的所有div.

I need to hide all divs except the 1rst div prior to user making a selection in the drop down.

基于下面的脚本,如何从除第一个选项之外的所有选项中获取值,以便隐藏除第一个div之外的所有div?

Based on the script below, how can I get the values from all the options except the first option so I can hide all divs except the first div?

$('.dd-show-hide').find('option:not(:first)').val().hide();

$('.dd-show-hide').change(function(){
   $(this).find('option').each(function(){
      $('#' + this.value).hide();
    });
    $('#' + this.value).show();
});

<select class="dd-show-hide">
    <option>Choose</option>
    <option value="div1">Show Div 1</option>
    <option value="div2">Show Div 2</option>
</select>

<div  id="div1" class="drop-down-show-hide">div 1</div>
<div  id="div2" class="drop-down-show-hide">div 2</div>

推荐答案

那么您可以使用first选择器和not方法:

Well you can use first selector and not method:

$('.drop-down-show-hide').not(':first').hide();

或触发更改事件,无需使用each方法.

Or trigger the change event, there is no need to use the each method.

var $divs = $('.drop-down-show-hide');
$('.dd-show-hide').change(function(){
   $divs.hide().filter('[id='+this.value+']').show();
}).change();

这篇关于首先选择所有选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆