How to open select programmatically

前端 未结 4 1476
醉梦人生
醉梦人生 2021-01-17 10:07

Does anyone know if it is possible to open a select programmatically in angularjs. Ive tried

angular.element(el).trigger(\'focus\');
angular.element(el).trig         


        
相关标签:
4条回答
  • 2021-01-17 10:16

    The answer provided by haste doesn't work:

    var cb = element[0].getElementsByClassName('some-selector')[0];
    var event = new MouseEvent('mousedown', {
        'view': window,
        'bubbles': true,
        'cancelable': true
    });                        
    cb.dispatchEvent(event);
    

    I use more simple method:

    var cb = element[0].getElementsByClassName('some-selector')[0];
    angular.element(cb).triggerHandler('click');
    
    0 讨论(0)
  • 2021-01-17 10:19

    try this

    function openSelect(selectId) {
        var event = new MouseEvent('mousedown', {
            'view': window,
            'bubbles': true,
            'cancelable': true
        });
        var cb = document.getElementById(selectId);
        cb.size=4;
        cb.dispatchEvent(event);
    }
    openSelect("testId");

    or read more here Triggering built-in events

    0 讨论(0)
  • 2021-01-17 10:31

    Try this for Hover:

    JS:

    $(document).ready(function(){
    $("#selectId").hover(function(){
       $(this)[0].size=$(this).find("option").length;
    });
    $("#selectId").click(function(){
       $(this)[0].size=0;
    });
    });
    

    HTML:

    <select id="selectId">
      <option>Volvo</option>
      <option >Saab</option>
      <option>Mercedes</option>
      <option>Audi</option>
    </select>
    
    0 讨论(0)
  • 2021-01-17 10:33

    i had same problem and finally i'm create my own directive:

    angular.module('openselect', [])
        .directive('openselect', function () {
            var showDropdown = function (element) {
                var event;
                event = document.createEvent('MouseEvents');
                event.initMouseEvent('mousedown', true, true, window);
                element.dispatchEvent(event);
            };
            return {
                restrict: 'A',
                scope: {
                    'elemento': '@'
                },
                link: function (scope, elem, attrs, ctrl) {
                    elem.bind('click', function () {
                        var elemento = document.getElementById(scope.elemento);
                        showDropdown(elemento);
                    });
                }
            };
        });
    

    To use:

    <div style="position:relative">
    <span ng-show="submittedForm.contry.$error.required && co_form.contry.$error.required" class="label label-danger">Indicanos tu país</span>
    <select name="country" class="form-control input-md" ng-model="formCheckout.country" id="myCountry" placeholder="España" required>
        <option value="6" selected="selected">España</option>
        <option value="1">Alemania</option>
        <option value="15">Portugal</option>
    </select>
    <span class="glyphicon glyphicon-chevron-down" style="position: absolute; right: 10px; color: #A6A6A6; top: 18px;" openselect elemento="myCountry"></span>
    

    Adds directive to any tag passing id of select (elemento) that you want to open.

    You can see the very basic javscript code inside directive if you want to use in other context;

    hope it helps ;D

    0 讨论(0)
提交回复
热议问题