简体   繁体   English

在div的JavaScript隐藏/显示上切换单选按钮

[英]Toggle radio button on javascript hide/show of div

I've struggled with this one for a couple days and my lack of depth in JavaScript is getting the best of me. 我已经为这一天苦苦挣扎了几天,而我对JavaScript的缺乏了解正在使我受益匪浅。 This project is in the "mock-up" stage. 该项目处于“模拟”阶段。 Briefly, the page starts with selecting a "series". 简要地说,该页面从选择“系列”开始。 Once selected we have several sections of "boxes" generated dynamically via PHP (eventually will come from database) with unique ID's for each box. 一旦选定,我们将通过PHP动态生成“框”的多个部分(最终将来自数据库),每个框具有唯一的ID。

Each Box in each category can independently be turned on/off via hiding the box with a simple JavaScript change of the box div's css display value. 通过使用box div的css显示值的简单JavaScript更改来隐藏该框,可以单独打开/关闭每个类别中的每个Box。 This is done as the user will need a visual representation of what they are turning On or Off. 之所以如此,是因为用户需要直观地表示他们正在打开或关闭的内容。

I need to keep track of each box's status as ON or OFF and when the user is done setting all the boxes as On or Off, they will submit the form (not coded yet) and the various radio button states will be acted on with future code not yet written. 我需要跟踪每个框的状态为“开”或“关”,当用户完成将所有框的状态设置为“开”或“关”时,他们将提交表单(尚未编码),并且将来会处理各种单选按钮状态代码尚未编写。 I am keeping track of this with a simple radio button "On" or "Off" for each box within each group. 我通过使用简单的单选按钮“打开”或“关闭”来跟踪每个组中的每个框。 When the boxes are dynamically built, each group assigns a unique name for the radio button group for the box and a unique ID for each radio button within the box group. 动态构建框时,每个组都会为框的单选按钮组分配一个唯一的名称,并为框组中的每个单选按钮分配一个唯一的ID。

I need the radio button to start with "On" checked for all boxes (ie all boxes are shown) and then if a box is clicked and hidden, the radio button for that box toggles from "On" to "Off" being checked. 我需要单选按钮以所有框都选中“开”开始(即显示所有框),然后如果单击并隐藏了一个框,则该框的单选按钮会从“开”切换为“关”。 Then it toggles back to On if the box is shown again. 如果再次显示该框,它将切换回“开”。 Adding "checked" to the HTML radio button input does not work due to the JS, so it needs to be added via JS. 由于JS,无法在HTML单选按钮输入中添加“已选中”,因此需要通过JS添加。

I understand how to change a single radio button state via JavaScript. 我了解如何通过JavaScript更改单个单选按钮的状态。 Where I am struggling is how to set it up to deal with the dynamic boxes and act on each box as a unique action to set the initial state of "checked" for the "On" radio button and then swapping the values on hide/show. 我正在努力解决的问题是如何设置它以处理动态框,并在每个框上作为唯一动作来为“打开”单选按钮设置“已选中”的初始状态,然后交换隐藏/显示的值。 I know I probably need to use "this" in my JS, but... well, like I said, this is my weak area in JS and I'm am looking for guidance to learn. 我知道我可能需要在JS中使用“ this”,但是...就像我说的那样,这是我在JS中的薄弱环节,我正在寻找学习的指南。

Thanks in advance to everyone for helping to educate me! 在此先感谢大家对我的帮助!

HTML/PHP HTML / PHP

<?php
/*Test Data */
$series140 = array(
    'st' => 2,
    'rt' => 3,
    'ibt' => 4,
    'dt' => 5,
    'lpjmh' => 6,
    'sjo' => 7,
    'mp' => 8,
    'mmo' => 9,
    'sd' => 8,
    'irp' => 7,
    'pl' => 6,
    'kl' => 5,
    'scbk' => 4,
    'dcbk' => 3,
    'ocgt' => 2
    );
$series220 = array(
    'st' => 2,
    'rt' => 3,
    'ibt' => 4,
    'dt' => 5,
    'lpjmh' => 6,
    'sjo' => 7,
    'mp' => 8,
    'mmo' => 9,
    'sd' => 8,
    'irp' => 7,
    'pl' => 6,
    'kl' => 5,
    'scbk' => 4,
    'dcbk' => 3,
    'ocgt' => 2
    );
$series240 = array(
    'st' => 2,
    'rt' => 3,
    'ibt' => 4,
    'dt' => 5,
    'lpjmh' => 6,
    'sjo' => 7,
    'mp' => 8,
    'mmo' => 9,
    'sd' => 8,
    'irp' => 7,
    'pl' => 6,
    'kl' => 5,
    'scbk' => 4,
    'dcbk' => 3,
    'ocgt' => 2
    );
$series440 = array(
    'st' => 2,
    'rt' => 3,
    'ibt' => 4,
    'dt' => 5,
    'lpjmh' => 6,
    'sjo' => 7,
    'mp' => 8,
    'mmo' => 9,
    'sd' => 8,
    'irp' => 7,
    'pl' => 6,
    'kl' => 5,
    'scbk' => 4,
    'dcbk' => 3,
    'ocgt' => 2
    );
$series540 = array(
    'st' => 2,
    'rt' => 3,
    'ibt' => 4,
    'dt' => 5,
    'lpjmh' => 6,
    'sjo' => 7,
    'mp' => 8,
    'mmo' => 9,
    'sd' => 8,
    'irp' => 7,
    'pl' => 6,
    'kl' => 5,
    'scbk' => 4,
    'dcbk' => 3,
    'ocgt' => 2
    );
$series640 = array(
    'st' => 2,
    'rt' => 3,
    'ibt' => 4,
    'dt' => 5,
    'lpjmh' => 6,
    'sjo' => 7,
    'mp' => 8,
    'mmo' => 9,
    'sd' => 8,
    'irp' => 7,
    'pl' => 6,
    'kl' => 5,
    'scbk' => 4,
    'dcbk' => 3,
    'ocgt' => 2
    );
$series740 = array(
    'st' => 2,
    'rt' => 3,
    'ibt' => 4,
    'dt' => 5,
    'lpjmh' => 6,
    'sjo' => 7,
    'mp' => 8,
    'mmo' => 9,
    'sd' => 8,
    'irp' => 7,
    'pl' => 6,
    'kl' => 5,
    'scbk' => 4,
    'dcbk' => 3,
    'ocgt' => 2
    );
$series2100 = array(
    'st' => 2,
    'rt' => 3,
    'ibt' => 4,
    'dt' => 5,
    'lpjmh' => 6,
    'sjo' => 7,
    'mp' => 8,
    'mmo' => 9,
    'sd' => 8,
    'irp' => 7,
    'pl' => 6,
    'kl' => 5,
    'scbk' => 4,
    'dcbk' => 3,
    'ocgt' => 2
    );
$series3300 = array(
    'st' => 1,
    'rt' => 0,
    'ibt' => 4,
    'dt' => 5,
    'lpjmh' => 3,
    'sjo' => 1,
    'mp' => 2,
    'mmo' => 1,
    'sd' => 2,
    'irp' =>47,
    'pl' => 4,
    'kl' => 2,
    'scbk' => 3,
    'dcbk' => 3,
    'ocgt' => 2
    );

if (isset($_POST['build'])) {
    $series = $_POST['doorSeries'];
    switch ($series) {
        // BUILD SERIES FUNCTION TO PULL FROM DATABASE IN FUTURE CODE
        case 140:
        $series = $series140;
        break;
        case 220:
        $series = $series220;
        break;
        case 240:
        $series = $series240;
        break;
        case 440:
        $series = $series440;
        break;
        case 540:
        $series = $series540;
        break;
        case 640:
        $series = $series640;
        break;
        case 740:
        $series = $series740;
        break;
        case 2100:
        $series = $series2100;
        break;
        case 3300:
        $series = $series3300;
        break;
        default:
        echo "not a valid selection";
        exit;
        break;
    }
}
function seriesBlocks($blockType) {
    if (is_array($blockType)) {
        $category = '';
        foreach ($blockType as $key => $value) {
            $categoryName = $key;
            $category .= '<h1>' . $categoryName . ' ' . $value . '</h1>';
            // $category .= '<p>show all | hide all</p>';
            for ($i=0; $i < $value; $i++) {
                $blockName = $categoryName . 'Block' . $i;
                $category .= "<a href=javascript:ReverseDisplay('". $blockName ."') class='shopLink'>";
                $category .= 'Click to show/hide.';
                $category .= '<input type="radio" name="' . $blockName . '" id="' . $blockName . 'On" value="on" >On';
                $category .= '<input type="radio" name="' . $blockName . '" id="' . $blockName . 'Off" value="off">Off';
                $category .= '<div id="' . $blockName . '" class="shopBlock">';
                $category .= '<h1>' . $blockName . '</h1>';
                $category .= '<p>Blah Blah Blah</p>';
                $category .= '</div></a>';
            }
            $category .= '<div class="clearfix"></div>';
        }
    }
    return $category;
}

 ?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
<script type="text/javascript" src="javascript.js"></script>
<link rel="stylesheet" href="style.css">
    <title>Shop Drawings</title>
</head>
<body>

<!-- Testing area -->
<h1>Testing Area</h1>
<?php 
    print_r($series140);
 ?>
<div class="clearfix"></div>
<!-- End Testing Area -->
<br />

<form name="BuildShopDrawing" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="doorSeries">Select Door Series to Build:</label>
    <select name="doorSeries" id="doorSeries">
        <option value="140">140</option>
        <option value="220">220</option>
        <option value="240">240</option>
        <option value="440">440</option>
        <option value="540">540</option>
        <option value="640">640</option>
        <option value="740">740</option>
        <option value="2100">2100</option>
        <option value="3300">3300</option>
    </select>
    <input type="submit" name="build" id="build" value="Start the Build">
</form>

    <?php 
    if ($_POST) {
        echo seriesBlocks($series);
        echo $series;
    }
 ?>

</body>
</html>

JavaScript JavaScript的

function ReverseDisplay(d) {
    if(document.getElementById(d).style.display == "none") { 
        document.getElementById(d).style.display = "block";
        document.getElementById(d + "On").checked = true;
     }
    else {
        document.getElementById(d).style.display = "none";
        document.getElementById(d + "Off").checked = true;
    }
}

CSS CSS

.shopLink {
    display: block;
    float: left;
    background-color: #ccc;
    margin: 10px;
}
.clearfix {
    clear: both;
}
h1 {
    margin-bottom: 0;
}

Kept at it and got the light bulb to go on. 坚持下去,让灯泡继续点亮。

Changed JS to: 将JS更改为:

function reverseDisplay(d) {
    if(document.getElementById(d).style.display == "none") { 
        document.getElementById(d).style.display = "block";
        document.getElementById(d + "On").checked = true;
     }
    else {
        document.getElementById(d).style.display = "none";
        document.getElementById(d + "Off").checked = true;
    }
}

After that, set the default of the "On" radio button to "checked" in the dynamically created HTML so just the PHP function changed to: 之后,在动态创建的HTML中将“打开”单选按钮的默认值设置为“选中”,以便仅将PHP函数更改为:

function seriesBlocks($blockType) {
    if (is_array($blockType)) {
        $category = '';
        foreach ($blockType as $key => $value) {
            $categoryName = $key;
            $category .= '<h1>' . $categoryName . ' ' . $value . '</h1>';
            // $category .= '<p>show all | hide all</p>';
            for ($i=0; $i < $value; $i++) {
                $blockName = $categoryName . 'Block' . $i;
                $category .= "<a href=javascript:reverseDisplay('". $blockName ."') class='shopLink'>";
                $category .= 'Click to show/hide.';
                $category .= '<input type="radio" name="' . $blockName . '" id="' . $blockName . 'On" value="on" checked>On';
                $category .= '<input type="radio" name="' . $blockName . '" id="' . $blockName . 'Off" value="off">Off';
                $category .= '<div id="' . $blockName . '" class="shopBlock">';
                $category .= '<h1>' . $blockName . '</h1>';
                $category .= '<p>Blah Blah Blah</p>';
                $category .= '</div></a>';
            }
            $category .= '<div class="clearfix"></div>';
        }
    }
    return $category;

All is working as desired now. 现在一切正常。

Thank you to everyone for your assist. 谢谢大家的协助。 Most of the comments lead me in directions that helped me figure this out. 大多数评论将我引向帮助我解决这一问题的方向。 Greatly Appreciated! 不胜感激!

I recommend you use jquery. 我建议您使用jQuery。 then you can write something like. 那么你可以写类似的东西。

$("option").change(function(){
   $(this).dowhatever
   do whatever
});

EDIT: I'm also not sure if I completely understand what you're trying to accomplish. 编辑:我也不确定我是否完全理解您要完成的工作。 I'm happy to try and help if I get a clearer understanding 如果能获得更清晰的理解,我很乐于尝试并提供帮助

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

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