简体   繁体   English

HTML Select,JavaScript动态加载

[英]HTML Select, Javascript dynamic load

I have the following: 我有以下几点:

  <div class="form-group"> <select required name="select_search"> <option value="Titel">Titel</option> <option value="Autor">Autor</option> <option value="Datum">Datum</option> </select> </div> <div class="form-group"> <input type="text" class="form-control" id="search_query" placeholder="Suchbegriff eingeben" name="search_query"/> </div> 

But what I want is the following: The input field shouldn't be always there, I'm looking for a solution to dynamically load the "item" under the select, when one of the first two options is chosen, the textbox should appear, when you choose the 3rd entry, then a calendar should appear (Datum means date, so you should be able to choose a date in a calendar). 但是我想要的是以下内容: input字段不应该一直存在,我正在寻找一种解决方案以动态加载select下的“ item”,当选择了前两个选项之一时,文本框应该出现,当您选择第三个条目时,将出现一个日历(“基准”表示日期,因此您应该能够在日历中选择一个日期)。 Maybe I also have to carry about, that nothing is prechosen in the select-box, otherwise it's a bit bizarre, when theres something chosen but nothing has appeared to enter sth. 也许我还必须忍受,选择框中什么都没有被预选,否则,当选择了某些东西但似乎没有东西进入时,这有点奇怪。

HTML Code: HTML代码:

<div class="form-group">
                <select name="select_search" onchange="selectedValue(this)">
                    <option value="" >Select Option</option>
                    <option value="1">Titel</option>
                    <option value="2">Autor</option>
                    <option value="3">Datum</option>
                </select>
            </div>
            <div class="form-group">
                <input type="text" class="form-control" id="search_query" style="display:none;" placeholder="Suchbegriff eingeben"
                       name="search_query"/>
            </div>

JavaScript Code: JavaScript代码:

var element = document.getElementById('search_query');
function selectedValue(_this) {
element.style.display = '';
switch (_this.value) {
    case '1':
        element.type = 'text';
        break;
    case '2':
        element.type = 'text';
        break;
    case '3':
        element.type = 'date';
        break;
    default:
        element.style.display = 'none';
  }
}

I Hope this will help you :) 我希望这能帮到您 :)

you want something like this 你想要这样的东西

var select = document.getElementsByTagName('select')[0];
var input = document.getElementsByTagName('input')[0];

select.addEventListener('change', function () {
    var value = select.value;

    if (value === 'Titel' || value === 'Autor') {
        input.style.display = 'block';
    } else {
        input.style.display = 'none';

        // do datepicker stuff
    }
});

fiddle - 小提琴 -

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

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