简体   繁体   English

根据下拉选择显示隐藏的div

[英]show hidden div based on dropdown selection

I have a Member Registration Form that contains a dropdown. 我有一个包含下拉列表的会员注册表。 It is for events so firstly they enter their membership details, select an event and then the number of tickets they wish to purchase. 这是为了事件,所以他们首先输入他们的会员资料,选择一个事件,然后选择他们想要购买的门票数量。 The options are as follows: 选项如下:

  • Member 会员
  • Member + 1 Guest 会员+ 1位游客
  • Member + 2 Guests 会员+ 2位客人
  • Member + 3 Guests, and so on. 会员+ 3位客人,依此类推。

What I want to happen is that because "Members" are FREE to attend, I want to keep the payment div hidden unless they select Member + xxxx. 我想要发生的是因为“会员”可以免费参加,我希望隐藏付款div,除非他们选择会员+ xxxx。

I am a complete novice when it comes to javascript/jquery but I can understand most of it when its written. 当谈到javascript / jquery时,我是一个完整的新手,但是当它写完时我能理解它的大部分内容。

I have a JSfiddle ( http://jsfiddle.net/bEuRh/ ) created with some basics but I need a hand writing the script. 我有一个JSfiddle( http://jsfiddle.net/bEuRh/ )创建了一些基础知识,但我需要一个手写脚本。 Some of the code is below: 部分代码如下:

<div id="tickets">
<label for="CAT_Custom_255276">Number of Tickets: <span class="req">*</span></label>
<select class="cat_dropdown" id="CAT_Custom_255276" name="CAT_Custom_255276">
    <option value=" ">-- Please select --</option>
    <option value="Member">Member</option>
    <option value="Member + 1 Guest">Member + 1 Guest</option>
    <option value="Member + 2 Guests">Member + 2 Guests</option>
    <option value="Member + 3 Guest">Member + 3 Guests</option>
</select>
</div>
<div id="payMethod">
<div class="header">
    PAYMENT METHOD
</div>
<div id="payOptions">
    <div id="payInfo">
        <div id="pay0" class="paymentinfo">
            <p>PAYMENT INFO OPTION 1</p>
        </div>
        <div id="pay1" class="paymentinfo">
            <p>PAYMENT INFO OPTION 2</p>
        </div>
        <div id="pay2" class="paymentinfo">
            <p>PAYMENT INFO OPTION 3</p>
        </div>
    </div>
</div>
</div>

If anyone could please assist, that would be greatly appreciated. 如果有人可以请求协助,那将非常感激。

Here is a working example using pure JS: 这是一个使用纯JS的工作示例:

//when a new option is selected...
document.getElementById('CAT_Custom_255276').addEventListener('change', function () {
    //hide all .paymentinfo
    Array.prototype.forEach.call(document.querySelectorAll('.paymentinfo'),  function (e) {
        e.style.display = 'none';
    });
    //get the selected index - 2 (first two do not impact display)
    var sel = +this.selectedIndex - 2;
    //if it is one of the last 3 selected...
    if (sel >= 0) {
        //display payMethod and the chosen individual options
        document.getElementById('payMethod').style.display = 'block';
        document.getElementById('pay' + sel).style.display = 'block';
    }
    else {
        //otherwise hide payMethod
        document.getElementById('payMethod').style.display = 'none';
    }
});

http://jsfiddle.net/bEuRh/4/ http://jsfiddle.net/bEuRh/4/

I like Raymond's approach of changing the HTML to: 我喜欢Raymond改变HTML的方法:

    <option value="0">-- Please select --</option>
    <option value="1">Member</option>
    <option value="2">Member + 1 Guest</option>
    <option value="3">Member + 2 Guests</option>
    <option value="4">Member + 3 Guests</option>

Do that, and then use this jQuery to keep the payment div hidden unless they select the option for more than one member: 这样做,然后使用此jQuery保持支付div隐藏,除非他们为多个成员选择选项:

$('.cat_dropdown').change(function() {
    $('#payMethod').toggle($(this).val() >= 2);
});

I like this approach for several reasons: 我喜欢这种方法有几个原因:

  1. it makes sense to give each option a value that corresponds to the meaning of the selection. 为每个选项赋予与选择含义相对应的值是有意义的。 For example, Member + 1 Guest has an associated value of 2 . 例如, Member + 1 Guest的关联值为2

  2. it balances the code between the markup and the javascript in a meaningful, simpler way. 它以有意义,更简单的方式平衡标记和javascript之间的代码。

  3. Instead of tons of javascript to worry about, you only have these three lines of jQuery to maintain. 而不是担心大量的javascript,你只需要维护这三行jQuery。

Here's a jsfiddle to illustrate: http://jsfiddle.net/martco/3TvwV/4/ 这里有一个jsfiddle来说明: http//jsfiddle.net/martco/3TvwV/4/

http://jsfiddle.net/bEuRh/5/ http://jsfiddle.net/bEuRh/5/

I'll get the value when select change is detected and compare.. 检测到选择更改后我会得到值并进行比较..

EDIT: 编辑:

$("select").change(function () {
    var str = "";
    str = parseInt($(this).val());

    $("#payMethod").toggle(str >= 2)
});

In HTML, i changed the value to integer... 在HTML中,我将值更改为整数...

        <option value="0">-- Please select --</option>
        <option value="1">Member</option>
        <option value="2">Member + 1 Guest</option>
        <option value="3">Member + 2 Guests</option>
        <option value="4">Member + 3 Guests</option>

if you wanna show individual div... you can try this one.. 如果你想展示个人...你可以尝试这个...

http://jsfiddle.net/bEuRh/6/ http://jsfiddle.net/bEuRh/6/

It's better to attache events in script instead of onchange in html but then I'd have to add a lot of JS code just to make it browser compatible: 最好在脚本中附加事件而不是在html中使用onchange ,但是为了使浏览器兼容,我必须添加大量的JS代码:

<script type="text/javascript">
function showInfo(){
    var divs=document.querySelectorAll(".paymentinfo"),
    i,
    // next line assumes only one element with this style
    select=document.querySelectorAll(".cat_dropdown")[0], 
    index;
    for(i=0;i<divs.length;i++){
        divs[i].style.display="none";
    }
    //get the index of the select
    index=select.selectedIndex-2;
    if(index>-1){
        divs[index].style.display="block";
        document.getElementById('payMethod').style.display = 'block';
        return
    }
    document.getElementById('payMethod').style.display = 'none';
}
</script>

<div id="tickets">
<label for="CAT_Custom_255276">Number of Tickets: <span class="req">*</span></label>
<select class="cat_dropdown" id="CAT_Custom_255276" name="CAT_Custom_255276" onchange="showInfo();">
    <option value=" ">-- Please select --</option>
    <option value="Member">Member</option>
    <option value="Member + 1 Guest">Member + 1 Guest</option>
    <option value="Member + 2 Guests">Member + 2 Guests</option>
    <option value="Member + 3 Guest">Member + 3 Guests</option>
</select>
</div>
<div id="payMethod" style="display:none">
<div class="header">
    PAYMENT METHOD
</div>
<div id="payOptions">
    <div id="payInfo">
        <div id="pay0" class="paymentinfo" style="display:none">
            <p>PAYMENT INFO OPTION 1</p>
        </div>
        <div id="pay1" class="paymentinfo" style="display:none">
            <p>PAYMENT INFO OPTION 2</p>
        </div>
        <div id="pay2" class="paymentinfo" style="display:none">
            <p>PAYMENT INFO OPTION 3</p>
        </div>
    </div>
</div>
</div>

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

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