javascript - Jquery 将下拉列表转换为使用 onchange 事件进行选择

标签 javascript jquery

我想转换下拉菜单

<ul class="cat-items selectdropdown">
    <li class="cat-item cat-item-25">
        <a href="http://restaurantapplianceparts.com/?product_cat=a1-compressor" title="View all posts filed under A-1 Compressor">A-1 Compressor</a>
    </li>
    <li class="cat-item cat-item-207">
        <a href="http://restaurantapplianceparts.com/?product_cat=world-hand-dryer" title="View all posts filed under World Hand Dryer">World Hand Dryer</a>
    </li>
</ul>

动态选择菜单。这是我正在使用的,它看起来很完美。但是没有进行转换,并且显示下拉列表而不是选择。这是在准备就绪时加载的 jquery 代码。

$('ul.selectdropdown').each(function() {
        var list = $(this),
            select = $(document.createElement('select')).insertBefore($(this).hide());
            $(select).attr("style","cursor:pointer;display: inline-block; width:99%;");
        $('>li', this).each(function() {
            var ahref = $(this).children('a'),
                target = ahref.attr('target'),
                option = $(document.createElement('option')).appendTo(select).val(ahref.attr('href')).html(ahref.html());

        });
    });

一旦显示 select,我将使用 onchange 使其工作。

$('select').bind('change',function () {
   //Redirect Page
});

如有任何帮助,我们将不胜感激。这里 id 演示网址:restaurantapplianceparts.com 艾哈迈尔。

最佳答案

我使用了您的代码并进行了一些更改以使其正常工作。

HTML

<ul class="cat-items selectdropdown">
    <li class="cat-item cat-item-25">
        <a href="http://restaurantapplianceparts.com/?product_cat=a1-compressor" title="View all posts filed under A-1 Compressor">A-1 Compressor</a>
    </li>
    <li class="cat-item cat-item-207">
        <a href="http://restaurantapplianceparts.com/?product_cat=world-hand-dryer" title="View all posts filed under World Hand Dryer">World Hand Dryer</a>
    </li>
</ul>

Javascript

$('ul.selectdropdown').each(function() {
        var list = $(this),
            select = $(document.createElement('select')).insertBefore($(this).hide());
            $(select).attr("style","cursor:pointer;display: inline-block; width:99%;");
        $('>li', this).each(function() {
            var ahref = $(this).children('a'),
                target = ahref.attr('target'),
                option = $(document.createElement('option')).appendTo(select).val(ahref.attr('href')).html(ahref.html());

        });
    $(select).change(function() {
        window.location.href = $(this).find('option:selected').val();
    });
    });

jsFiddle:http://jsfiddle.net/Cf7Pt/1/

关于javascript - Jquery 将下拉列表转换为使用 onchange 事件进行选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21731261/

相关文章:

javascript - 为什么for循环后面加了分号仍然执行?

jquery - 当标题为空时如何禁用线索提示?

javascript - 搜索时定位两个字段

javascript - 如何防止IE11弹窗(您确定要离开此页面吗)

javascript - 如何将文件附加到 html 'file' 元素以使用 jquery/js 上传?

javascript - iPhone HTML5 应用程序滚动问题

javascript - 如何在 keydown 上启动计时器并在 keyup 上停止?

javascript - Apache:什么可能会去除空格并以其他方式更改 .js 文件?

javascript - 变量变量(使用另一个变量的名称创建变量)

javascript - 替换折叠固定标题中的图像(滚动) - javascript