Javascript函数只适用于一个变量

Javascript function works only on one variable

本文关键字:一个 变量 函数 适用于 Javascript      更新时间:2023-09-26

我正在开发一个插件,但它现在只工作了一半。问题是,我从复选框中获得了一些变量,但该函数只对复选框的最后一个选中项(列表中)有效,而对其他项无效。

我的代码:

jQuery(document).ready(function(){
  jQuery('#gap-filtedit input[type=button]').click(function(){
    jQuery('.filter-checkbox:checked').each(function() {
      getchecked = jQuery(this).data('rel');
      val = '[gap ';
      console.log(getchecked);
      jQuery('.' + getchecked).each(function(){
        val = val + getchecked + '=';
        name = jQuery(this).find('label').data('id');
        title = jQuery(this).find('input[type=text],select').val();
        if (!title) title = '';
        if (name != 'null') {
          val = val + name + ':' + title + ', ';
        }
      });
    });

    window.send_to_editor( val );
  });
});

日志将为我提供所选的选项。但是,console.log之后的函数将仅对行中最后一个选定的函数起作用。

如何使该功能适用于每个选定的项目?

在.each语句中执行函数:

jQuery(document).ready(function(){
        jQuery('#gap-filtedit input[type=button]').click(function(){
            jQuery('.filter-checkbox:checked').each(function() {
                    getchecked = jQuery(this).data('rel');
            val = '[gap ';
            console.log(getchecked);
jQuery('.' + getchecked).each(function(){   
        val = val + getchecked + '=';                      
        name = jQuery(this).find('label').data('id');
        title = jQuery(this).find('input[type=text],select').val();     
                    if (!title) title = '';
                        if (name != 'null') {
                val = val + name + ':' + title + ', ';
                window.send_to_editor( val );
                            }
                        });
                    });

                });
            });

循环使用的复选框时

val = '[gap '; // setting the value

在循环附加的表单字段时

...
val = val + getchecked + '='; // appending
...
val = val + name + ':' + title + ', '; // appending
...

现在,当内部循环结束并返回到外部循环时,val被设置回[gap(基本上擦除最后的内部循环附加)

解决方案是在click函数中声明变量,并在第一个循环中附加(而不是设置)

示例jsfidd

jQuery(document).ready(function () {
    jQuery('#gap-filtedit input[type=button]').click(function () {
        var val = ""; // give variable proper scope vs creating a global
        jQuery('.filter-checkbox:checked').each(function () {
            getchecked = jQuery(this).data('rel');
            val += '[gap '; // append instead of set
            console.log(getchecked);
            jQuery('.' + getchecked).each(function () {
                val += getchecked + '=';
                var name = jQuery(this).find('label').data('id');
                var title = jQuery(this).find('input[type=text],select').val();
                if (!title) title = '';
                if (name != undefined) {
                    val += name + ':' + title + ', ';
                }
            });
        });
        window.send_to_editor(val);
    });
});

旁注:

  • 确定其他变量(var titlevar name)的范围
  • var name = jQuery(this).find('label').data('id');将具有值或是undefined(而不是"null"