javascript - 如何在加载时在页面的下拉列表中选择事件项目

标签 javascript laravel

converting this UL to dropdown

 <ul class="list-group showdiv">
             <a href="developmentService"><li  class="list-group-item {{{ (Request::is('developmentService') ? 'active' : '') }}}">Bespoke         Software Development</li></a>
     <a href="UserExperienceService"><li  class="list-group-item {{{ (Request::is('UserExperienceService') ? 'active' : '') }}}">User Experience   Design</li></a>
     <a href="StaffService"><li  class="list-group-item {{{ (Request::is('StaffService') ? 'active' : '') }}}">Staff Augmentation</li></a>
     <a href="TestingService"><li  class="list-group-item {{{ (Request::is('TestingService') ? 'active' : '') }}}">Testing and Validation</li></a>
     <a href="GamingService"><li  class="list-group-item {{{ (Request::is('GamingService') ? 'active' : '') }}}">Gaming</li></a>
 </ul>

To convert Dropdown

Converting ul by this script.

     <script type="text/javascript">  $(function() {
         $('ul.showdiv').each(function() {
             var $select = $('<select />');
             $(this).find('a').each(function() {
                 var $option = $('<option />');
                 $option.attr('value', $(this).attr('href')).html($(this).html());
                 $select.append($option);
             });
             $(this).replaceWith($select);
         });

         $('select').on('change', function (e) {    
         var valueSelected = this.value;    
         window.location.href = valueSelected;  });  });

如何在页面的下拉列表中将“事件”项目启用为“selected=true”。 ** 它总是选择下拉列表中的第一项

最佳答案

也许这样的东西有效:(在每个循环内)

$('ul.showdiv').each(function() {
    var $select = $('<select />');
    $(this).find('a').each(function() {
        var $option = $('<option />');
        $option.attr('value', $(this).attr('href')).html($(this).html());
        if($(this).children().hasClass('active')){
            $option.prop('selected', true);
        }
        $select.append($option);
    });
    $(this).replaceWith($select);
});

关于javascript - 如何在加载时在页面的下拉列表中选择事件项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27752825/

相关文章:

JavaScript 仅在 Squarespace 中被调用一次

javascript - 有没有办法找出用户拥有的最高角色?

JavaScript 删除上方元素

javascript - jquery div 移动使用 css 和面板更改

laravel - PhpStorm 将 undefined variable 错误用于传递变量以查看

php - Laravel 通过表名获取模型

javascript - node.js 中的异步编程是否可以加速 CPU 密集型任务?

php - 如果抛出异常,Laravel 数据库事务会发生什么?

javascript - 为什么这个对 Controller 的 ajax 调用在特定页面 (Laravel) 中不起作用?

javascript - Jquery、ajax : Clicking on a button and getting the value of the cell next to it on the same row