简体   繁体   English

Javascript表单中的多个输入

[英]Multiple input in a form in Javascript

I want to create an assign form, for example user select teacher A for subject A but user can press a button to add more teacher to subject A. I heard this can be done by Javascript but I dont know how.我想创建一个分配表单,例如用户为subject A选择teacher A ,但用户可以按下按钮为subject A添加更多教师。我听说这可以通过Javascript完成,但我不知道如何。

                  <label class="col-sm-2 col-form-label">Teacher</label>
                    <div class="form-group">
                     <select class="form-control" name="teachername">
                      <option>Sarah</option>
                      <option>Adam</option>
                      <option>Mark</option>
                      <option>Anderson</option>
                      <option>Wood</option>
                     </select>
                    </div>
                    <div class="ml-auto p-2">   
                        <button type="button" class="btn btn-primary">+</button>
                    </div>
                </div>```

If you want user/student A to select multiple teachers for Subject Maths(example), you can use select with checkbox because the user doesn't know how many teachers are available for a particular subject.如果您希望用户/学生 A 为科目数学(示例)选择多名教师,您可以使用带有复选框的选择,因为用户不知道特定科目有多少名教师可用。

Further, you can also implement check/uncheck all functionality此外,您还可以实现选中/取消选中所有功能

 var expanded = false; var checkboxes = document.getElementById("checkboxes"); var inputs = checkboxes.getElementsByTagName('input') function showCheckboxes() { if (!expanded) { checkboxes.style.display = "block"; expanded = true; } else { checkboxes.style.display = "none"; expanded = false; } } function getSelectedCheckboxValues(name) { const checkboxe = document.querySelectorAll(`input[name="${name}"]:checked`); let values = []; checkboxe.forEach((checkbox) => { values.push(checkbox.value); }); return values; } function mySubmitFunction(e) { e.preventDefault(); console.log(getSelectedCheckboxValues('teacher')); return false; }
 .multiselect { width: 200px; } .selectBox { position: relative; } .selectBox select { width: 100%; font-weight: bold; } .overSelect { position: absolute; left: 0; right: 0; top: 0; bottom: 0; } #checkboxes { display: none; border: 1px #dadada solid; } #checkboxes label { display: block; } #checkboxes label:hover { background-color: #1e90ff; } .submitBtn { margin-top: 15px; cursor: pointer; }
 <form onsubmit="return mySubmitFunction(event)"> <div class="multiselect"> <div class="selectBox" onclick="showCheckboxes()"> <select> <option>Select an option</option> </select> <div class="overSelect"></div> </div> <div id="checkboxes"> <label for="first"> <input type="checkbox" name="teacher" value="first" id="first" />First Teacher</label> <label for="second"> <input type="checkbox" name="teacher" value="second" id="second" />Second Teacher</label> <label for="three"> <input type="checkbox" name="teacher" value="three" id="three" />Thrid Teacher</label> <label for="four"> <input type="checkbox" name="teacher" value="four" id="four" />Fourth Teacher</label> <label for="five"> <input type="checkbox" name="teacher" value="five" id="five" />Fifth Teacher</label> </div> <button type="submit" class="submitBtn">Submit</button> </div> </form>

Well.. IDK if this going to make you like it?好吧.. IDK 是否会让您喜欢它? I'm making the button to add a selected teacher into Array[] call "teachers" then remove selected teacher from options.我正在制作按钮以将选定的教师添加到 Array[] 调用“教师”,然后从选项中删除选定的教师。 I hope you can figure the rest?我希望你能弄清楚剩下的?

Here's my example: https://onecompiler.com/html/3xg4q9e3p这是我的例子: https : //onecompiler.com/html/3xg4q9e3p

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

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