如何在 jquery 中使用多个下拉菜单

纳吉

我想在我的网站上执行以下操作-

当我单击一个列表项时,它必须从具有 class 值的 span 标记中获取文本。因此,当我单击与该列表相关的按钮时,它应该将收集的文本附加到链接引用中。

目前,我的代码仅适用于第一个列表项。当我对下一个项目做同样的事情时,它给我来自第一个列表项目的数据。

这是我的代码-

$(document).ready(function() {
  var dropOneValue = "";
  $("ul.which-way").on("click", function() {
    $(this).find('li').toggleClass("open-list");
    $(this).find('open-list').css("display", "block");
  });
  $("li.cadja").on("click", function() {
    dropOneValue = ""
    $($(this).parent().find('.which-init')[0]).html($(this).html());
    handleDropdownOne();
  });
});
window.handleDropdownOne = function() {
  dropOneValue = $($($('.drowpdown-one').find('.which-init')[0]).find('span.value')[0]).text();
  console.log(dropOneValue);
};
handleDropdownOne();
$('a#trip').on("click", function() {
  $(this).attr("href", "https://www.westcoastway.co.za/" + dropOneValue);
});
.which-wrapper {
  width: 100%;
  max-width: 300px;
}

ul.which-way {
  margin: 0;
  list-style: none;
  background-color: grey;
  margin-bottom: 5px;
}

ul.which-way li:not(:first-child) {
  display: none;
}

.value {
  display: none !important;
}

ul.which-way {
  cursor: pointer;
  padding: 0;
}

.value {
  display: none;
}

li.open-list {
  display: block !important;
}

.find {
  margin-bottom: 10px;
  display: inline-block;
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="which-wrapper">
  <div class="drowpdown-one dropdown">
    <ul class="which-way">
      <li class="which-init">Unguided I-Day Return Trips</li>
      <li data-value="value 2" class="cadja"><span class="value">darling-wine-hops-day-by-which-way</span><span class="real">Darling Wine & Beer Trip</span></li>
      <li data-value="value 3" class="cadja"><span class="value">mamre-werf-khwa-ttu-culture-day-by-which-way-trips</span><span class="real">Culture & Adventure Trip</span></li>
      <li data-value="value 4" class="cadja"><span class="value">cape-west-coast-wildlife-fossil-trip</span><span class="real">Wildlife & Fossils Trip</span></li>
    </ul>
    <a href="#" target="__blank" id="trip" class="find">FIND YOUR TRIP</a>
  </div>
  <div class="drowpdown-one dropdown">
    <ul class="which-way">
      <li class="which-init">Guided I-Day Return Trips</li>
      <li data-value="value 2" class="cadja"><span class="value">5-day-west-coast</span><span class="real">5 Day West Coast Explorer</span></li>
      <li data-value="value 3" class="cadja"><span class="value">5-day-namaqua-spring-flower-tour</span><span class="real">5 Day Namaqualand Spring Flower</span></li>
    </ul>
    <a href="#" target="__blank" id="tour" class="find">FIND YOUR TRIP</a>
  </div>
</div>

用户代理

根据@madalin 的回答,让你的 jquery 像这样:

$(document).ready(function(){
    var dropOneValue = "";
            $("ul.which-way").on("click", function() {
            $(this).find('li').toggleClass("open-list");
            $(this).find('open-list').css("display", "block");
            });
            $("li.cadja").on("click", function(){
                console.log($(this).find('.value').html());
          dropOneValue = $(this).find('.value').html();
           $($(this).parent().find('.which-init')[0]).html($(this).html());
            });  

            $('a#trip').on("click", function(){
                $(this).attr("href", "https://www.westcoastway.co.za/"+dropOneValue);
            });

            $('a#tour').on("click", function(){
                $(this).attr("href", "http://capeytours.co.za/west-coast-tours/"+dropOneValue);
            });
    });

我只是将您在之前代码中的这段代码重新添加回来:

$($(this).parent().find('.which-init')[0]).html($(this).html());`

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

jQuery和HTML如何在下拉菜单中使用存储

如何在HTML中使用jQuery正确显示下拉菜单

如何在导航栏中使用多个下拉菜单?

如何使用JQuery创建下拉菜单

如何在现有网站中使用 jquery、html、css 在网站顶部创建下拉菜单

使用jQuery加载页面时如何隐藏多个下拉菜单?

在下拉菜单中使用jQuery

jQuery,如何使用同一类为多个下拉菜单获取先前选择的下拉列表值?

如何使用默认选择下拉菜单关闭自定义jQuery下拉菜单

如何在 NextJs 中使用 useState Hook 来创建多个下拉菜单?

如何使用常量类jQuery选择不同的下拉菜单

如何使用jQuery选择下拉菜单选项?

如何使用jQuery控制输入类型下拉菜单

如何使用jQuery检测下拉菜单中的按钮单击

如何使用jQuery验证表单中的下拉菜单?

如何使用自定义类一次性重置jQuery Mobile中的多个下拉菜单

如何在Flutter中使用对象创建下拉菜单

如何在jQuery中设置下拉菜单的SELECTED属性?

使用jQuery $ this下拉菜单

使用jQuery加载jQuery对话框后如何在下拉菜单中设置一些文本

如何使用Jquery控制下拉菜单选择选项菜单

如何使用jQuery创建带有嵌套子菜单的下拉菜单

如何将 jQuery 脚本用于表单中的多个选择类型下拉菜单?

jQuery下拉菜单允许下拉多个菜单

jQuery下拉菜单-如何在单击主体时隐藏菜单

如何使用用于jQuery的Bootstrap Multiselect插件更改下拉菜单的宽度?

如何使用选定的li列表项的文本更改下拉菜单中按钮的文本-jQuery

如何使用jquery在laravel中单击下拉菜单中的li值?

如何使用焦点jquery事件打开下拉菜单<select>?