简体   繁体   English

在选择输入中的选择选项上触发事件

[英]Trigger event on select option in select input

I have trouble in trigger event on select input.我在选择输入上触发事件时遇到问题。 I need to display input type text when option "Another" is selected.选择“另一个”选项时,我需要显示输入类型文本。 Here is the code:这是代码:

HTML: HTML:

<div class="form-group">
    <label class="control-label" for="affiliation">Occupation:</label>
    <select name="affiliation" id="affiliation" class="form-control required>
        <option value="Choose" selected>Choose</option>
        <option value="valueA">Student</option>
        <option value="valueB">Lecturer</option>
        <option value="valueC">Scientist</option>
        <option value="Another">Another</option>
     </select>
</div>
<div class="form-group hide">
     <input type="text" name="anotherV" id="anotherV" class="form-control" placeholder="Plese enter your occupation" required>
</div>

CSS: CSS:

.hide {display: none;}

I use bootstrap我使用引导程序

You have a missing closing double-quote in your HTML after form-control in your select.在您的选择中的form-control之后,您的 HTML 中缺少结束双引号。 Below that's fixed, and there's an ID added to the parent div of your hidden input field to make the code easier to understand, feel free to alter if you're comfortable.下面是固定的,并且在隐藏输入字段的父 div 中添加了一个 ID,以使代码更容易理解,如果您觉得舒服,可以随意更改。

HTML HTML

<div class="form-group">
    <label class="control-label" for="affiliation">Occupation:</label>
    <select name="affiliation" id="affiliation" class="form-control" required>
        <option value="Choose" selected>Choose</option>
        <option value="valueA">Student</option>
        <option value="valueB">Lecturer</option>
        <option value="valueC">Scientist</option>
        <option value="Another">Another</option>
     </select>
</div>
<div class="form-group hide" id="hiddenInputContainer">
     <input type="text" name="anotherV" id="anotherV" class="form-control" placeholder="Plese enter your occupation" required>
</div>

jQuery jQuery

 $( "#affiliation" ).change(function(event) {
        if ($(this).val() === "Another") {
            $('#hiddenInputContainer').toggleClass('hide');
        }
  });
$("#affiliation").change(function() {
   if (this.value === "Another") $(".hide input").show()
   else $(".hide input").hide()
})

You can use change() event handler with toggleClass()您可以将change()事件处理程序与toggleClass()

  1. Use change() for handling change event使用change()处理更改事件
  2. parent() for selecting parent of the element parent()用于选择元素的父级
  3. toggleClass() with flag for toggling the class hide toggleClass()带有用于切换类hide标志

DEMO :演示:

 $('#affiliation').change(function() { $('#anotherV').parent().toggleClass('hide', this.value != 'Another'); });
 .hide { display: none; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="form-group"> <label class="control-label" for="affiliation">Occupation:</label> <select name="affiliation" id="affiliation" class="form-control required"> <option value="Choose" selected>Choose</option> <option value="valueA">Student</option> <option value="valueB">Lecturer</option> <option value="valueC">Scientist</option> <option value="Another">Another</option> </select> </div> <div class="form-group hide"> <input type="text" name="anotherV" id="anotherV" class="form-control" placeholder="Plese enter your occupation" required> </div>

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

相关问题 如何选择更改事件上的选项并触发其他选择选项以进行响应? - How to Select an option on change event and trigger the other select option to respond? 如何防止onclick事件在选择选项中触发 - how to prevent onclick event to trigger in select option 如何使用 typescript 触发 select 元素到 select 第一个选项的事件? - How to trigger a event on a select element to select first option with typescript? 使用选项文本而不是jQuery中的选项值来触发选择框事件 - trigger select box event using option text not option value in jquery 文本输入上的 Select2 4.0.5 触发事件 - Select2 4.0.5 trigger event on Text input 输入值的事件处理程序和 select 选项一起 - Event Handler for Input values and select option together 选项从添加输入中选择JQuery事件 - Option Select JQuery Event from Add Input 当已经选择了选择选项时发生事件触发器Select2(多个选择选项) - Event Trigger Select2 when the select option is already selected (Multiple Select Option) 更改选定选项时处理选择列表以触发事件 - Handling select list to trigger an event when changing selected option ReactJS - 即使从选择下拉列表中选择了相同的选项,也会触发事件 - ReactJS - trigger event even if the same option is selected from select dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM