简体   繁体   中英

How to activate an input field only when a certain drop down menu item is selected

I want the <input> named other to be activated if the item with the same name in the dropdown is selected. If else, it will be deactivated.

<div class="dropdown">
  Choisir le type:
  <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#">Type</a>
  <input type="hidden" id="selectedFormation" name="selectedFormation" />
  <ul class="dropdown-menu">
    <li><a href="#" data-value="item1">item1</a></li>
    <li><a href="#" data-value="item2">item2</a></li>
    <li><a href="#" data-value="other">other</a></li>
  </ul>
</div>
<input type="text" class="form-control" name="other" id="other">
$(".dropdown-menu li a").click(function() {
  $(this)
    .closest(".dropdown")
    .find(".btn")
    .html($(this).text() + ' <span class="caret"></span>');
  var value2 = $(this).data("value");
  $("#selectedFormation").val(value2);
});

You can set disabled property to true/false based on the value . You can add the disabled attribute to the input as the dropdown does not have the other value selected on page load.

 $(".dropdown-menu li a").click(function(){ $(this).closest('.dropdown').find('.btn').html($(this).text()+' <span class="caret"></span>'); var value2=$(this).data("value"); $('#selectedFormation').val(value2); if(value2 == 'other') $('#other').attr('disabled', false); else $('#other').attr('disabled', true); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="dropdown"> Choisir le type : <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#">Type</a> <input type="hidden" id="selectedFormation" name="selectedFormation"/> <ul class="dropdown-menu"> <li><a href="#" data-value="item1">item1</a></li> <li><a href="#" data-value="item2">item2</a></li> <li><a href="#" data-value="other">other </a></li> </ul> </div> <input type="text" class="form-control" name="other" id="other" disabled> 

Add disabled property to the input. Now on click of the anchor tag , get the value and if it is other enable it or disable it

 $(".dropdown-menu li a").click(function() { $(this).closest('.dropdown').find('.btn').html($(this).text() + ' <span class="caret"></span>'); var value2 = $(this).data("value"); $('#selectedFormation').val(value2); // added code here if (value2 === 'other') { $('#other').prop('disabled', false) } else { $('#other').prop('disabled', true) } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="dropdown"> Choisir le type : <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#">Type</a> <input type="hidden" id="selectedFormation" name="selectedFormation" /> <ul class="dropdown-menu"> <li><a href="#" data-value="item1">item1</a></li> <li><a href="#" data-value="item2">item2</a></li> <li><a href="#" data-value="other">other </a></li> </ul> </div> <input type="text" disabled class="form-control" name="other" id="other"> 

Your best bet is to change it to a form select element because that has a built-in onchange event. Because you have it as a ul you would have to recreate that by adding classes to based on hovers which would require a lot more JS. See the code snippets for onchange on this page:

https://www.w3schools.com/jsref/event_onchange.asp

Modifying their code here to resemble your case:

<select id="mySelect" onchange="myFunction()">
  <option value="item1">Item 1
  <option value="item2">Item 2
  <option value="other">Other
</select>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("mySelect").value;
    document.getElementById("demo").innerHTML = "You selected: " + x;
    if (x === "other") {
        // enable form field code
    }
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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