简体   繁体   English

显示大于/小于选择更改时的隐藏字段集

[英]Show hide fieldsets greater than/less than on select change

I have a form which is going to contain 4 fieldsets. 我有一个将包含4个字段集的表单。 The user must fill in a minimum of 1 fieldset but can choose to complete additional fieldsets. 用户必须至少填写1个字段集,但可以选择完成其他字段集。 Each fieldset's contents are identical. 每个字段集的内容都是相同的。

I want them to be able to choose a number of fieldsets to show from a dropdown. 我希望他们能够从下拉列表中选择要显示的多个字段集。 On selecting a number from the dropdown the relevant number of fieldsets appears. 从下拉列表中选择一个数字后,将出现相关数目的字段集。

So if they choose 3 from the quantity dropdown, the first 3 fieldsets should be shown. 因此,如果他们从数量下拉列表中选择3,则应显示前3个字段集。 If they change their mind and drop to 2 fieldsets then the 3rd should hide again. 如果他们改变主意并下降到2个字段集,则第3个字段应再次隐藏。

Here's the base HTML. 这是基本的HTML。

<form>
    <select id="quantity" name="quantity">
        <option disabled="disabled">xx</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
    </select>

    <fieldset id="fieldset_name_1">Form Area 1</fieldset>
    <fieldset id="fieldset_name_2" style="display: none;">Form Area 2</fieldset>
    <fieldset id="fieldset_name_3" style="display: none;">Form Area 3</fieldset>
    <fieldset id="fieldset_name_4" style="display: none;">Form Area 4</fieldset>

</form>​

And here's my very basic jQuery so far. 到目前为止,这是我非常基础的jQuery。 I'm quite a noob to client side stuff, generally burying my head in php :) 我对客户端的东西相当陌生,通常将我的头埋在php中:)

var registerCount = function() {
    var qty = $(this).val();

    var fieldsets = $('fieldset');

    fieldsets.slice(0, qty ).show();  
}


$("#quantity").change(registerCount).keypress(registerCount);

So I've got it showing fieldsets correctly, I'm just not sure how to hide them on change or keypress again. 因此,我已经正确显示了字段集,但是我不确定如何在更改或再次按键时隐藏它们。

I'm sure this is simple but I think my familiarity with PHP keeps me trying to code things completely differently to the jQuery/javascript way. 我敢肯定这很简单,但是我认为我对PHP的熟悉使我试图以完全不同于jQuery / javascript的方式编写代码。

要隐藏未选中的内容,请在显示选中的内容之前隐藏所有内容:

fieldsets.hide().slice(0, +qty).show();

Try hide all first and then show according to selection: 尝试先隐藏全部,然后根据选择显示:

Sample: 样品:

var registerCount = function() {
    var qty = $(this).val();

    var fieldsets = $('fieldset');
    fieldsets.each(function (){
       $(this).hide();
    });

    fieldsets.slice(0, qty ).show();  
}

$("#quantity").change(registerCount).keypress(registerCount);

You can put the fieldset dynamically to not hide them. 您可以动态放置字段集以不隐藏它们。

$(function(){
    $('#quantity').change(function(){
        $('#fieldsets').html('');
        n = $(this).val();
        for (i=1; i<=n; i++){
            $('<fieldset/>', {
                id: 'fieldset_name_'+i,
                text: 'Form Area '+i
            }).appendTo('#fieldsets');
        }
    });
})​

Demo: http://jsfiddle.net/JmeYZ/ 演示: http//jsfiddle.net/JmeYZ/

Here is the answer, 这是答案,

http://jsfiddle.net/mQ23q/ http://jsfiddle.net/mQ23q/

Hope it will help 希望对你有帮助

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

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