jquery show hide div基于动态选择值

时间:2014-05-29 10:37:44

标签: javascript jquery html

我正在制作一个项目,需要根据选择显示/隐藏分部。 我需要选择隐藏我想要的组的女主角。最初,数量和金额不知道!目前适用于#selectField_1和box_1。但 我不想做一个我想让它变得动态的静态脚本

<script>
    $(document).ready(function (value) {
        $('.box_1').hide();

        $('#51').show(); <==================Need Selected value from <select> as Default

        $('#selectField_1').change(function () {
            $('.box_1').hide();
            $('#'+$(this).val()).show();
        });
    });
</script>

<select id="selectField_1">
    <option value="51" selected>option1</option>
    <option value="52">option2</option>
    <option value="53">option3</option>
    <option value="54">option4</option>
</select>

<select id="selectField_2">
    <option value="55" selected>option1</option>
    <option value="56">option2</option>
    <option value="57">option3</option>
    <option value="58">option4</option>
</select>

.......................................

<select id="selectField_n">
    <option value="n" selected>option1</option>
    <option value="n">option2</option>
    <option value="n">option3</option>
    <option value="n">option4</option>
</select>

</br> 

<div id="51" class="box_1">Content 51</div>
<div id="52" class="box_1">Content 52</div>
<div id="53" class="box_1">Content 53</div>
<div id="54" class="box_1">Content 54</div>

<div id="55" class="box_2">Content 55</div>
<div id="56" class="box_2">Content 56</div>
<div id="57" class="box_2">Content 57</div>
<div id="58" class="box_2">Content 58</div>

...........................................

<div id="n" class="box_n">Content n</div>
<div id="n" class="box_n">Content n</div>
<div id="n" class="box_n">Content n</div>
<div id="n" class="box_n">Content n</div>

4 个答案:

答案 0 :(得分:1)

在下拉菜单上申请课程:

<select id="selectField_1" class="SelectField">
        <option value="51" selected>option1</option>
        <option value="52">option2</option>
        <option value="53">option3</option>
        <option value="54">option4</option>
</select>

你必须这样做:

$('.SelectField').each(function(){
    $('.box_'+this.id.split('_')[1]).hide();
    $('#'+$(this).val()).show();
});

$('.SelectField').change(function () {
    $('.box_'+this.id.split('_')[1]).hide();
    $('#'+$(this).val()).show();
});

FIDDLE DEMO

答案 1 :(得分:0)

要按值设置选项,您需要使用:

$('#selectField_1 option[value="51"]');

并选择它:

$('#selectField_1 option[value="51"]').prop('selected', true);

答案 2 :(得分:0)

也许你可以这样做:

<select id="selectField_1">
    <option value="51" class="selected">option1</option>
    <option value="52">option2</option>
    <option value="53">option3</option>
    <option value="54">option4</option>
</select>

在你的js:

$("#your_select_id").find("option.selected").show()

答案 3 :(得分:0)

正如Ehsaan Sajjad所说,在下拉列表中使用Apply Class来访问所有下拉列表,例如

<select id="selectField_1" class="ddlClass">
    <option value="51" selected>option1</option>
    <option value="52">option2</option>
    <option value="53">option3</option>
    <option value="54">option4</option>
</select>
<select id="selectField_2" class="ddlClass">
    <option value="55" selected>option1</option>
    <option value="56">option2</option>
    <option value="57">option3</option>
    <option value="58">option4</option>
</select>

并在div中再添加一个类来默认隐藏所有div,如

<div id="51" class="box_1 boxdiv">Content 51</div>
<div id="52" class="box_1 boxdiv">Content 52</div>
<div id="53" class="box_1 boxdiv">Content 53</div>
<div id="54" class="box_1 boxdiv">Content 54</div>

<div id="55" class="box_2 boxdiv">Content 55</div>
<div id="56" class="box_2 boxdiv">Content 56</div>
<div id="57" class="box_2 boxdiv">Content 57</div>
<div id="58" class="box_2 boxdiv">Content 58</div>

在CSS写中

.boxdiv{ display: none;}

现在你的jQuery代码看起来像

$('.ddlClass').change(function () {

        $('.box_'+this.id.split('_')[1]).hide();
        $('#'+$(this).val()).show();
    });