简体   繁体   English

如果使用css选择器在jquery中选择了选项,则显示div

[英]display div if option is selected in jquery with css selectors

I have a drop down list with 4 surface type options. 我有一个包含4种表面类型选项的下拉列表。 The fist option is the default and is a shown div with input boxes for user to enter dimensions, the rest of the other options are hidden on page load via css. fist选项是默认选项,显示的div带有用于输入尺寸的输入框,供用户输入尺寸,其余的其他选项则通过css隐藏在页面加载中。 Currently, if I select a non-rectangle option, it does not hide the default div and show the hidden div. 当前,如果我选择一个非矩形选项,它不会隐藏默认的div并显示隐藏的div。 Here's the html: 这是html:

    <div class="calc-wrap">
    <h3>Soil and Mulch</h3>
    <label for="surface">Type of surface:</label>

   <select id="surface" name="surface" onchange="selected_surface('calc_id', this.value)">
    <option value="_rect" selected="selected">Rectangle</option>
    <option value="_circle">Circle</option>
    <option value="_ring">Ring</option>
    <option value="_triangle">Triangle</option>
</select>

    <div class="user-inputs">
    <div id="rectangle">

        <div class="calc-input">
            <label for="length"><strong>Length</strong>(ft):</label>
            <input id="length" name="length" text="text" />
        </div><!--end of calc-input-->

        <div class="calc-input">
            <label for="width"><strong>Width</strong> (ft):</label>
            <input id="width" name="width" type="text" />
        </div><!--end of calc-input-->
    </div><!--end of rectangle-->

    <div class="hidden_field" id="circle">
        <div class="calc-input">
            <label for="radius"><strong>Radius</strong>(ft.):</label>
            <input id="radius" name="radius" type="text" />
        </div><!--end of calc-input-->
    </div><!--end of circle-->

    <div class="hidden_field" id="ring">
        <div class="calc-input">
            <label for="in_diameter"><strong>Inside Diameter</strong>(ft.):</label>
            <input id="in_diameter" name="indiameter" type="text" />
        </div><!--end of calc-input-->
        <div class="calc-input">
            <label for="ex_diameter"><strong>Outer Diameter</strong>(ft.):</label>
            <input id="ex_diameter" name="exdiameter" type="text" />
        </div><!--end of calc-input-->
</div><!--end of ring-->

    <div class="hidden_field" id="triangle">
            <div class="calc-input">
                <label for="base"><strong>Base</strong>(ft.):</label>
                <input id="base" name="base" type="text" />
            </div><!--end of calc-input-->
            <div class="calc-input">
                <label for="height"><strong>Height</strong>(ft.):</label>
                <input id="height" name="height" type="text" />
            </div><!--end of calc-input-->
        </div><!--end of triangle-->


        <div class="calc-input" id="soil_mulch_depth">
            <label for="soil_mulch_depth"><strong>Depth</strong>(inches):</label>
            <input id="soil_mulch_depth" name="depth" type="text" />
        </div><!--end of soil_mulch_depth-->
</div><!--end user_inputs-->

        <div class="results">
            <div class="calc-result">
                <label><strong>Volume</strong>(yards&sup3;)</label>
                <div class="result-value" id="soil_mulch_result"></div>
            </div><!--end calc-result-->
            <br>
            <div class="calc-result">
                <label><strong>Number of bags</strong>:</label>
                <div class="result-value" id="soil_bags_needed">0</div>
                <label>of soil</label>
                <input class="soil_parts" id="bags_soil" name="bags_soil" type="hidden" value="25" />
            </div><!--end result-value-->
            <br>
            <div class="calc-result">
                <label><strong>Number of bags:</strong>

                </label>
                <div class="result-value" id="mulch_bags_needed">0</div><!--end result-value-->
                <label>of mulch</label>
                <input class="mulch_parts" id="bags_mulch" name="bags_mulch" type="hidden" value="9" />
            </div>
            <br>
            <button class="calc-submit" onclick="bulk_calculate(" soil_mulch ",0)">Calculate</button>

Here's the css: 这是CSS:

.hidden_field {
    display:none;
}

.user-inputs {
    float:left;
    margin-right:20px;
}
.results {
    float:left;
}
.calc-wrap {
   margin:30px 0;
   line-height:1;
}
.calc-wrap select {
   margin:0;
}
.calc-wrap input {
   margin:0 0 9px;
   font-family:arial;
   width:100px;
}
.calc-submit {
   clear:both;
   display:block;
}
.calc-wrap label {
    display:inline-block;
    vertical-align:middle;
    width:170px;
    font-size: 14px;
}
.calc-result label {
    width:auto;
    white-space:nowrap;
}
.result-value {
    display:inline-block;
    vertical-align:middle;
}
.calc-wrap h3 {
    font-weight:bold;
    color: #8dc63f;
    font-family:'Tahoma', sans-serif;

}

Here's the jquery: 这是jQuery:

    function selected_surface(calc_id, show_id){
    $ = jQuery;
    $('#' + calc_id + '_rect').css('display', 'none');
    $('#' + calc_id + '_circle').css('display', 'none');
    $('#' + calc_id + '_ring').css('display', 'none');
    $('#' + calc_id + '_triangle').css('display', 'none');
    $('#' + calc_id + '_manual').css('display', 'none');
    $('#' + calc_id + show_id).css('display', 'block');
    }

see my fiddle here: http://jsfiddle.net/rnle99/8nKFg/5/ 在这里看到我的小提琴: http : //jsfiddle.net/rnle99/8nKFg/5/

I've tried looking at previous questions such as: 我尝试查看以前的问题,例如:

show DIV once selected option PASS from drop down menu 从下拉菜单中选择“通过”后显示DIV

Display div if option is selected in jQuery 如果在jQuery中选择了选项,则显示div

  • first of of you're passing calc_id onchange="selected_surface('calc_id', this.value) i've no idea what that is. 首先,您要传递calc_id onchange="selected_surface('calc_id', this.value)我不知道那是什么。
  • i've changed the <option> values to rect , circle etc for simplicity. 为了简单起见,我将<option>值更改为rectcircle等。
  • i've added rect id for the div for rectangle, which was missing. 我为矩形的div添加了rect ID,该ID丢失了。
  • i've added .shape class to all divs for each shape to access them together. 我已经将.shape类添加到所有div中,以便每个形状可以一起访问它们。

      function selected_surface(elm){ var divId= $(elm).val(); $('.shape').hide(); // hide all other shapes $('#'+divId).css('display','block'); // show the selected shape } 

JSFiddle 的jsfiddle

Couple issues with your code... 您的代码有几个问题...

Mainly, the values being passed from options when the change event fires don't match up to the IDs of the hidden containers. 主要是,更改事件触发时从选项传递的值与隐藏容器的ID不匹配。

Here's a fiddle which works. 这是一个有效的小提琴。 I've simplified the selector logic, changes the html slightly and have everything executing on document ready. 我简化了选择器逻辑,略微更改了html,并准备好在文档中执行的所有操作。 Have a look: http://jsfiddle.net/adamfullen/2Kk38/ 看看: http : //jsfiddle.net/adamfullen/2Kk38/

HTML HTML

<div class="calc-wrap">
     <h3>Soil and Mulch</h3>
     <label for="surface">Type of surface:</label>

    <select id="surface" name="surface" onchange="">
        <option value="rectangle" selected="selected">Rectangle</option>
        <option value="circle">Circle</option>
        <option value="ring">Ring</option>
        <option value="triangle">Triangle</option>
    </select>

    <div class="user-inputs">
        <div class="hidden_field" id="rectangle">

            <div class="calc-input">
                <label for="length"><strong>Length</strong>(ft):</label>
                <input id="length" name="length" text="text" />
            </div><!--end of calc-input-->

            <div class="calc-input">
                <label for="width"><strong>Width</strong> (ft):</label>
                <input id="width" name="width" type="text" />
            </div><!--end of calc-input-->
        </div><!--end of rectangle-->

        <div class="hidden_field" id="circle">
            <div class="calc-input">
                <label for="radius"><strong>Radius</strong>(ft.):</label>
                <input id="radius" name="radius" type="text" />
            </div><!--end of calc-input-->
        </div><!--end of circle-->

        <div class="hidden_field" id="ring">
            <div class="calc-input">
                <label for="in_diameter"><strong>Inside Diameter</strong>(ft.):</label>
                <input id="in_diameter" name="indiameter" type="text" />
            </div><!--end of calc-input-->
            <div class="calc-input">
                <label for="ex_diameter"><strong>Outer Diameter</strong>(ft.):</label>
                <input id="ex_diameter" name="exdiameter" type="text" />
            </div><!--end of calc-input-->
    </div><!--end of ring-->

        <div class="hidden_field" id="triangle">
                <div class="calc-input">
                    <label for="base"><strong>Base</strong>(ft.):</label>
                    <input id="base" name="base" type="text" />
                </div><!--end of calc-input-->
                <div class="calc-input">
                    <label for="height"><strong>Height</strong>(ft.):</label>
                    <input id="height" name="height" type="text" />
                </div><!--end of calc-input-->
            </div><!--end of triangle-->


            <div class="calc-input" id="soil_mulch_depth">
                <label for="soil_mulch_depth"><strong>Depth</strong>(inches):</label>
                <input id="soil_mulch_depth" name="depth" type="text" />
            </div><!--end of soil_mulch_depth-->
    </div><!--end user_inputs-->

            <div class="results">
                <div class="calc-result">
                    <label><strong>Volume</strong>(yards&sup3;)</label>
                    <div class="result-value" id="soil_mulch_result"></div>
                </div><!--end calc-result-->
                <br>
                <div class="calc-result">
                    <label><strong>Number of bags</strong>:</label>
                    <div class="result-value" id="soil_bags_needed">0</div>
                    <label>of soil</label>
                    <input class="soil_parts" id="bags_soil" name="bags_soil" type="hidden" value="25" />
                </div><!--end result-value-->
                <br>
                <div class="calc-result">
                    <label><strong>Number of bags:</strong>

                    </label>
                    <div class="result-value" id="mulch_bags_needed">0</div><!--end result-value-->
                    <label>of mulch</label>
                    <input class="mulch_parts" id="bags_mulch" name="bags_mulch" type="hidden" value="9" />
                </div>
                <br>
                <button class="calc-submit" onclick="bulk_calculate(" soil_mulch ",0)">Calculate</button>

JavaScript JavaScript的

function selected_surface(calc_id, show_id){
    var $ = jQuery;
    $('.hidden_field').hide();
    $('#'+show_id).show();
}

$(function(){
    $('#surface').on("change", function(e){
        var show_id = this.value;
        selected_surface('calc_id', show_id);        
    });

    $('#rectangle').show();

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM