简体   繁体   English

动态显示/隐藏动态生成 <div> s

[英]Dynamically showing/hiding dynamically-generated <div>s

I am using PHP to dynamically generate a number of <select> fields with two <div> s underneath each. 我正在使用PHP动态生成许多<select>字段,每个字段下面都有两个<div> An option is automatically selected and the <div> s are automatically shown/hidden by PHP based on the initial condition, but how can I dynamically switch which <div> is shown/hidden based on a <select> change, in addition to making a form field in that div either disabled or enabled? PHP将根据初始条件自动选择一个选项,并且<div>由PHP自动显示/隐藏,但是除了进行以下操作之外,如何根据<select>变化动态切换显示/隐藏哪个<div>div的表单字段是禁用还是启用?

For some background, I am displaying all of the information on the page that could be used, but the div s hide each option (using display:none ) that isn't being used, and each unused div also has a disabled form field. 对于某些背景,我将在页面上显示所有可以使用的信息,但是div隐藏未使用的每个选项(使用display:none ),并且每个未使用的div都有一个禁用的表单字段。 When a user selects the other option in the select box, the currently shown div needs to be hidden, the select inside the div needs to be disabled, and the new div needs to be shown and its select needs to be enabled. 当用户在选择框中选择另一个选项时,需要隐藏当前显示的div需要禁用div内部的select ,并且需要显示新的div并启用其select I'm doing this because each of the hidden/shown form fields has the same name as I'm posting them to a PHP script. 我这样做是因为每个隐藏/显示的表单字段都具有与将它们发布到PHP脚本相同的名称。

All of the <div> s are in the format of <div id="textdiv-uniqueID"> OR <div id="seldiv-uniqueID"> (where uniqueID is a unique number for each set of div s), and each select is in the format of <select name="uniqueID-good_value"> , where the uniqueID is the same as the corresponding div s. 所有<div>都采用<div id="textdiv-uniqueID"><div id="seldiv-uniqueID"> (其中uniqueID是每组div的唯一编号)的格式,并且每个select的格式为<select name="uniqueID-good_value"> ,其中uniqueID与对应的div相同。

To recap: I have a dynamically-generated set of select boxes and form fields. 回顾一下:我有一组动态生成的选择框和表单字段。 Each select box has two associated form fields, each in its own <div> , one of which will be hidden/disabled and the other which will be shown/enabled. 每个选择框都有两个关联的表单字段,每个都在自己的<div> ,其中一个将被隐藏/禁用,另一个将被显示/启用。 When the other option in a given select box is selected, the divs and form fields need to switch roles: the currently hidden/disabled one needs to be shown/enabled, and vice versa. 在给定选择框中选择另一个选项时,div和表单字段需要切换角色:需要显示/启用当前隐藏/禁用的选项,反之亦然。

Here's a jsFiddle with the basics, including my beginning attempt at figuring out the Javascript/jQuery. 这是一个具有基础知识的jsFiddle ,包括我最初尝试弄清Javascript / jQuery的尝试。

In general, something similar to what you need looks like this: 通常,类似于您所需的内容如下所示:

$('#uniqueID-good_value').on('change',function (){
    var val= $(this).val();
    if(val==1) {
        $('textdiv-uniqueID').show();
        $('seldiv-uniqueID').hide();
    }else{
        $('textdiv-uniqueID').hide();
        $('seldiv-uniqueID').show();
    }
});

for your case, you need to work a bit on the logic to make it work smart with the uniqueid. 对于您的情况,您需要做一些逻辑上的工作,以使其与uniqueid一起智能地工作。

Can't you just put a class of 'active' or 'hidden' on the relevant fields, and then swap the class when things change? 您不能只是在相关字段上放置一个“活动”或“隐藏”类,然后在情况发生变化时交换该类吗?

For instance: 例如:

$(".active").removeClass("active").addClass("hidden"); $(“。active”)。removeClass(“ active”)。addClass(“ hidden”);

You could put that into an event on your select widgets (I'm not sure what all the available events are, but there must be something like onselect or onselected, and there's always onmouseup) 您可以将其放入选择的小部件中的事件中(我不确定所有可用的事件是什么,但是必须有onselect或onselected之类的东西,并且始终存在onmouseup)

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

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