简体   繁体   English

Javascript 操作显示:无不能正常工作

[英]Javascript manipulation of display:none doesn't work properly

I'm trying to make a JS price calculator and I'm stuck on one part, where I do not find a solution for a couple of hours.我正在尝试制作一个 JS 价格计算器,但我被困在一个部分,几个小时内我都找不到解决方案。 I will appreciate your help.我会感谢你的帮助。

This is what I'm trying to do:这就是我想要做的:

  1. Displaying part, where there are 3 product types ( <input type="radio" ).显示部分,其中有 3 种产品类型( <input type="radio" )。 Clicking to one of them should appear with own LightBox and also hiding other parts of the <input> form.单击其中一个应该会出现自己的 LightBox,并且还会隐藏<input>表单的其他部分。
<div>
    <h2>Basic Price</h2>
    <div id="singleSpotLB" style="display:block;">
        <?php include "/Templates/singleSpotLightBox.php"?>
    </div>
    <div id="multiSpotLB" style="display: none;">
        <?php include "/Templates/multiSpotLightBox.php"?>
    </div>
    <div id="photoSilkLB" style="display: none;">
        <?php include "/Templates/photoSilk.php"?>
    </div>

    <label for="type">Transfer Type</label><br>
    <div>
        <label for="sSpot">
            <input type="radio" name="type" id="sSpot" onclick="invisibleColors()"> SingleSpot
        </label>
        <label for="mSpot">
            <input type="radio" name="type"id="mSpot" onclick="invisibleColors()"> MultiSpot
        </label>
        <label for="pSilk">
            <input type="radio" name="type"id="pSilk" onclick="invisibleColors()"> PhotoSilk
        </label>
    </div>
</div>
  1. Javascript part for hiding (by style.display = "none"; ) the divs which do not belong to the chosen product: Javascript 部分用于隐藏(通过style.display = "none"; )不属于所选产品的 div:
function invisibleColors() {
    let sSpot = document.getElementsByName("type");
    let singleSpotLB = document.getElementById("singleSpotLB");
    let multiSpotLB = document.getElementById("multiSpotLB");
    let photoSilkLB = document.getElementById("photoSilkLB");

    if (sSpot[0].checked) {
        document.getElementById('colorDiv').style.display = "none";
        singleSpotLB.style.display = "block";
        multiSpotLB.style.display = "none";
        photoSilkLB.style.display = "none";
    } else if (sSpot[1].checked) {
        document.getElementById('colorDiv').style.display = "block";
        singleSpotLB.style.display = "none";
        multiSpotLB.style.display = "block";
        photoSilkLB.style.display = "none";
    } else if (sSpot[2].checked) {
        document.getElementById('colorDiv').style.display = "none";
        singleSpotLB.style.display = "none";
        multiSpotLB.style.display = "none";
        photoSilkLB.style.display = "block";

    }
}

**The problem: When I'm testing the code it works well at the beginning but after a couple of clicks the label texts change properly, but the images just don't load. **问题:当我测试代码时,它一开始运行良好,但点击几下后, label文本正确更改,但图像无法加载。

Thanks, and I hope someone could help.谢谢,我希望有人可以提供帮助。

PS Content of the PHP files from where the LighBox is loaded:加载 LighBox 的 PHP 文件的 PS 内容:

<label for="type">SingleSpot</label>
    <div class="masonry-thumbs grid-container grid-3 clearfix">
        <a class="grid-item" href="http://ttransferhall.de/images/portfolio/4/1.jpg" data-lightbox="image">
            <div class="grid-inner">
                <img src="http://ttransferhall.de/images/portfolio/4/1.jpg" alt="Single Image">
                <div class="bg-overlay">
                    <div class="bg-overlay-content dark">
                        <i class="icon-line-plus h4 mb-0" data-hover-animate="fadeIn"></i>
                    </div>
                    <div class="bg-overlay-bg dark" data-hover-animate="fadeIn"></div>
                </div>
            </div>
        </a>

    </div>

Here is a video of the result这是结果的视频

@Alexandar Dimov Your code in Html is owner than the code in javascript. @Alexandar Dimov 您在 Html 中的代码比 javascript 中的代码拥有者。 add your style in other file CSS and try again.在其他文件 CSS 中添加您的样式,然后重试。

I found that there is an ajax process loaded from the template, which conflicts with the plain JS script.我发现有一个从模板加载的 ajax 进程,与普通的 JS 脚本冲突。 Thank you all for your opinions and help.谢谢大家的意见和帮助。

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

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