2016-07-15 3 views
0

Ich habe eine Multi-Select-Dropdown-Liste erstellt. Wenn derzeit eine Eingabe ausgewählt wird und der Benutzer auf die Beschriftung klickt, um eine Auswahl zu treffen, wird die Dropdown-Liste nicht mehr angezeigt. Wenn sie auf das Kontrollkästchen klicken, bleibt die Liste im Fokus. Ich möchte mich auf die Dropdown-Liste konzentrieren, bis der Benutzer seine Auswahl getroffen hat und auf die Schaltfläche "Senden" klickt. unabhängig davon, wo sie innerhalb der Liste klicken. Ich habe verschiedene Kombinationen von Klick-/Fokus-Listener-Funktionen ausprobiert und spiele mit den Bootstrap-Attributen data, aber nichts scheint zu funktionieren.Wie konzentriert man sich auf eine Dropdown-Liste?

 function ddInputsChanged($inputs, $outputWrapper) { 
 
      var $inputs = $('#ddDistrict input'); 
 
      var $outputWrapper = $('#lblDistrict'); 
 
      var outputLabel = 'All'; 
 
      var firstChecked = true; 
 

 
      $inputs.each(function() { 
 
      if (this.checked) { 
 
       var currentLabel = $(this).next('label').text(); 
 

 
       if (firstChecked == true) { 
 
       firstChecked = false; 
 
       outputLabel = currentLabel; 
 
       } else 
 
       outputLabel = outputLabel + ', ' + currentLabel; 
 
      } 
 
      }); 
 

 
      $outputWrapper.text(outputLabel); 
 
     }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> 
 

 
<div class="col-lg-12"> 
 
    <div class="button-group" style="padding: 7px 0px 7px 0px; margin-left: 18px;"> 
 
    <button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-user"></span>&nbsp;Districts&nbsp;<span class="caret"></span> 
 
    </button> 
 
    <ul id="ddDistrict" class="dropdown-menu"> 
 
     <li> 
 
     <input type="checkbox" id="inpDist1" value="1"> 
 
     <label for="inpDist1" class="input small" data-value="1">District 1 (Porter)</label> 
 
     </li> 
 
     <li> 
 
     <input type="checkbox" id="inpDist2" value="2"> 
 
     <label for="inpDist2" class="input small" data-value="2">District 2 (Jones Jr.)</label> 
 
     </li> 
 
     <li> 
 
     <input type="checkbox" id="inpDist3" value="3"> 
 
     <label for="inpDist3" class="input small" data-value="3">District 3 (Horne)</label> 
 
     </li> 
 
     <li> 
 
     <input type="checkbox" id="inpDist4" value="4"> 
 
     <label for="inpDist4" class="input small" data-value="4">District 4 (Haddaway)</label> 
 
     </li> 
 
     <li> 
 
     <input type="checkbox" id="inpDist5" value="5"> 
 
     <label for="inpDist5" class="input small" data-value="5">District 5 (Duncan)</label> 
 
     </li> 
 
     <li> 
 
     <input type="checkbox" id="inpDist6" value="6"> 
 
     <label for="inpDist6" class="input small" data-value="6">District 6 (Willner)</label> 
 
     </li> 
 
     <li> 
 
     <input type="checkbox" id="inpDist7" value="7"> 
 
     <label for="inpDist7" class="input small" data-value="7">District 7 (Brady)</label> 
 
     </li> 
 
     <li> 
 
     <button type="button" class="btn btn-default btn-sm btn-info center-block" onclick="ddInputsChanged('#ddDistrict input', '#lblDistrict');">Submit</button> 
 
     </li> 
 
    </ul> 
 
    </div> 
 
</div> 
 

 
<div class="row"> 
 
    <div class="col-lg-12"> 
 
    <div id="lblDistrict"></div> 
 
    </div> 
 
</div>

Antwort

0

Dies schien den Trick in einem kleinen Test zu tun. Stop-Propagation hinzugefügt und Code hinzugefügt, um das Menü nach dem Senden zu schließen.

$(document).on('click', '.dropdown-menu', function (e) { 
    e.stopPropagation(); 
}); 

function ddInputsChanged($inputs, $outputWrapper) { 
      var $inputs = $('#ddDistrict input'); 
      var $outputWrapper = $('#lblDistrict'); 
      var outputLabel = 'All'; 
      var firstChecked = true; 

      $inputs.each(function() { 
      if (this.checked) { 
       var currentLabel = $(this).next('label').text(); 

       if (firstChecked === true) { 
       firstChecked = false; 
       outputLabel = currentLabel; 
       } else 
       outputLabel = outputLabel + ', ' + currentLabel; 
      } 
      }); 

      $outputWrapper.text(outputLabel); 

    $(".open").removeClass('open'); 
     } 
+0

Brilliant !! Schnelle Frage ... woher kam die Klasse? –

+0

Es ist, was jquery ui hinzufügt, wenn es geöffnet wird. – Nikki9696

Verwandte Themen