【问题标题】:add text area on selecting options in dropdown list in java script在 java 脚本的下拉列表中选择选项时添加文本区域
【发布时间】:2021-03-31 09:14:35
【问题描述】:

function showInsertQuest(){
    var x = document.getElementById("one");
    if (x.style.display === "none"){
        x.style.display = "block";
    } else {
        x.style.display = "block";
    }
}


const target = document.querySelector("#second");

const displayWhenSelected = (source, value, target) => {
    const selectedIndex = source.selectedIndex;
    const isSelected = source[selectedIndex].value === value;
    target.classList[isSelected
        ? "add"
        : "remove"
    ]("show");
};
if(source= document.querySelector("#location"))
source.addEventListener("change", (evt) =>
    displayWhenSelected(source, "loc5", target)
);
body {
  align-items: center;
}
#container {
  border: 4px solid;
  position: auto;
  height: 650;
  width: 700px;
}
.main {
  border-bottom: solid;
  margin-bottom: 0px;
}
h2 {
  margin-left: 10px;
  margin-bottom: 1%;
  color: darkcyan;
  font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
}
#bottom {
  border-top: 0.7solid;
  margin-top: 0px;
}
button {
  margin-left: 10px;
  margin-right: 10px;
  border-radius: 4px;
  background-color: darkcyan;
  color: white;
  height: 35px;
  width: 80px;
  margin-top: -25px;
}

/* Insert Button */

.insert-btn {
  margin-left: 0px;
  margin-top: 480px;
  color: grey;
  margin-bottom: 0px;
  padding-left: 510.53px;
  border-top: none;
  height: 40px;
  font-weight: 200;
  font-size: 1.05rem;
}


.insert-btn:hover {
  background: grey;
  box-shadow: var(--shadow);
  color: white;
}

.insert-btn:active {
  background: grey;
  color: black;
  border: transparent;
}

#one {
  width: 700px;
  position: fixed;
  margin-top: 10px;
  display: none;
}

#one input {
  width: 0px;
  border: 1px solid;
  border-radius: 5px;
  display: inline;
  padding-right: 500px;
  padding-left: 7px;
  padding-top: 5px;
  padding-bottom: 5px;
  color: grey;
}

#one #Multiplechoice {
  margin-right: 0px;
  display: inline;
  border: none;
  position: fixed;
  height: 19px;
}

i {
  padding-left: 12px;
  padding-right: 5px;
  padding-top: 1px;
  color: grey;
}
#one .on {
  padding-left: 9px;
}

#one #second {
  margin-left: 22px;
  margin-top: 5px;
  display: none;
}
#one #second .show {
  display: block;
}
<div id="container">
    <div class="main">
        <h2>ADD NEW CALL</h2>
    </div> 
       
    <section class="insert-quest" id="insertquestone">
        <div id="one" >
            <div class="on">
                1 <input type="text"/><i class="fa fa-adn" aria-hidden="true"></i>

                <!-- DROPDOWN -->
                <select id="location" name="drop">
                    <option value="Select">Select</option>
                    <option value="loc5" >Multi-line text</option>
                    <option value="loc6" >Single choice</option>
                    <option value="loc7">Multi choice</option>      
                </select>
            </div>
            <!-- TEXTAREA -->
            <textarea name="term" id="second" cols="50" rows="3"></textarea>        
       </div>
   </section>

    <div id="content">

        <!-- Insert Quest Button -->
        <input type="button" value="+ADD NEW QUESTION" class="insert-btn" id="insertbtn" onclick="showInsertQuest()"/>
    </div>
    <div id="bottom">
        <h3><button>SAVE</button> cancel </h3>
    </div>
</div>

更新:我修改了您问题中的代码以提高可读性@Adam P.

尝试从下拉列表中选择多行文本时获取文本区域 类似地在选择单选时在问题下方添加 1 个文本框 并在选择单选时在问题下方添加 5 个文本框...

出现无法读取 null 的属性“addEventListener”的错误

【问题讨论】:

  • 你使用querySelector时是否加载了DOM?
  • 嗨舒布。请通过应用此处描述的概念How to Ask 来改进您的问题。 Not YELLING 是一种受欢迎的风格。提供更多信息、解释、您自己的想法的详细信息、您自己的调试尝试和分析实验的结果将非常有助于为您提供帮助。此外,许多用户感谢用户提出的问题,这些用户至少花费了tour 的努力,以表明愿意理解和接受这个社区的概念。这样做也可以避免系统要求在相当大的代码中显示更多的非代码。

标签: javascript css list dropdown onselect


【解决方案1】:

要使 textarea 可见,您必须做几件事:

在JS部分,改变

if(source= document.querySelector("#location"))

const source = document.querySelector("#location");

这个变化是不言自明的,你需要在附加事件监听器时定义节点。

在 CSS 更改中:

#one #second {
  margin-left: 22px;
  margin-top: 5px;
  display: none;
}
#one #second .show {
  display: block;
}

#second {
  margin-left: 22px;
  margin-top: 5px;
  display: none;
}
#second.show {
  display: block;
}

你不需要#one 选择器,一个id 选择器就足够了。此外,当您向已定义 id 的元素添加类 (.show) 时,您需要编写该类,且 CSS 中的 id 名称和类名称之间没有空格。

编辑:您可能正在将脚本加载到&lt;head&gt; 标记中。这样,脚本无法附加 eventListener,因为 DOM 元素尚不可用。要解决此问题并删除 Cannot read property 'addEventListener' of null 错误,您需要在 &lt;/body&gt; 结束之前加载脚本,如下所示:

<html>
    <head>
        <style></style>
    </head>
    <body>
        ...content of your web page
        <script></script>
    </body>
</html>

【讨论】:

  • 我按照你说的做了,但是错误“无法读取 null 的属性 'addEventListener'”
  • 我在评论的编辑部分添加了解决方案
  • wooooohhhhh .....它现在工作得很好......谢谢你
猜你喜欢
  • 2019-09-06
  • 1970-01-01
  • 2012-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多