简体   繁体   English

如何在一个页面上两次使用此多选下拉列表,以便每个实例输出到其自己的数组?

[英]How can I use this multiselect dropdown twice on one page so each instance outputs to its own array?

I want to place two separate multiselct dropdown menus on one page for a form I am designing. 我想在一个页面上放置两个单独的multiselct下拉菜单,用于我正在设计的表单。

I found this lovely dropdown with checkboxes that I would like to use: 我找到了这个可爱的下拉列表,我想使用复选框:

https://codepen.io/bseth99/pen/fboKH https://codepen.io/bseth99/pen/fboKH

I am trying to figure out the best way to run two instances of this dropdown on the same page, so that each dropdown's selections are saved in their own array. 我试图找出在同一页面上运行此下拉列表的两个实例的最佳方法,以便每个下拉列表的选择都保存在它们自己的数组中。

var options = [];

$( '.dropdown-menu a' ).on( 'click', function( event ) {

   var $target = $( event.currentTarget ),
       val = $target.attr( 'data-value' ),
       $inp = $target.find( 'input' ),
       idx;

   if ( ( idx = options.indexOf( val ) ) > -1 ) {
      options.splice( idx, 1 );
      setTimeout( function() { $inp.prop( 'checked', false ) }, 0);
   } else {
      options.push( val );
      setTimeout( function() { $inp.prop( 'checked', true ) }, 0);
   }

   $( event.target ).blur();

   console.log( options );
   return false;
});

Right now, if I place two instances of the button's HTML on my page, both dropdowns will modify the same "options" array because it's using a class selector for dropdown-menu. 现在,如果我在页面上放置按钮HTML的两个实例,则两个下拉列表都将修改相同的“options”数组,因为它使用了一个类选择器来显示下拉菜单。 I know I can use an ID selector instead of a class selector, and then just use the same block of jQuery over again with a different array, but is that really the best way to do this? 我知道我可以使用ID选择器而不是类选择器,然后使用不同的数组再次使用相同的jQuery块,但这真的是最好的方法吗? I am new to jQuery but that seems bloated. 我是jQuery的新手但看起来很臃肿。

$( '.dropdown-menu a' ).on( 'click', function( event ) {

   var $target = $( this ),
       val = $target.data( 'value' ),
       $inp = $target.find( 'input' ),
       idx;

   var options = $target.closest('.dropdown-menu').data('options') || [];


   if ( ( idx = options.indexOf( val ) ) > -1 ) {
      options.splice( idx, 1 );
      setTimeout( function() { $inp.prop( 'checked', false ) }, 0);
   } else {
      options.push( val );
      setTimeout( function() { $inp.prop( 'checked', true ) }, 0);
   }

   $target.closest('.dropdown-menu').data('options', options);

   $( event.target ).blur();

   return false;
});

console.log($( '.dropdown-menu:eq(0)' ).data('options'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将每个 scss 文件编译到它自己的文件夹中而不将它们合并成一个? - How can I compile each scss file into its own folder without merging them into one? 我创建了多个过滤器功能,每个功能都做自己的工作,但是,我不确定如何将它们链接起来,以便它们一起工作 - I've created multiple filters functions and each one does its own job right, however, I'm not sure how to chain them so they all work together 如何使每个用户都有自己的状态? - How do I make it so each user has its own state? 使用JavaScript,我试图将用户单击的每个页面的url存储到数组中,以便可以将该数组用作后退按钮 - Using JavaScript, I'm trying to store the url of each page the user clicks into an array so i can use that array for a back button jQuery插件的每个实例如何具有自己的属性和方法? - How can each instance of a jQuery plugin have its own properties and methods? 如何为数组中的每个对象分配自己的密钥? - How can i give each of the objects in my array their own key? 如何在一页上运行 javascript/Canvas 脚本两次? - How can i run a javascript/Canvas script twice on one page? 从字符串数组创建表,以便每个字符串都是其自己的列,而每一行都是该字符串的字母 - Create table from Array of Strings so that each string is its own column and each row is a letter of the string 如何为复选框下拉列表添加multiselect.js或sumoselect - How can I add multiselect.js or sumoselect for checkbox dropdown 如何让每个实例继承自己的成员实例? - How do I get each instance to inherit their own instance of a member?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM