0

我有一个通过 .click 函数检索的变量,我想将它传递给另一个函数,但我无法正确传递它,因为当我调用另一个函数时没有任何反应。

var className;
var flavor;
(function ($) {

  Drupal.myTest = Drupal.myTest|| {};

  Drupal.behaviors.myTest= {

    attach: function (context, settings) {

          Drupal.myTest.myFunction= function (className) {
            var passedVar= className;
            alert(passedVar);
          };



          $('.buttons').on('click', function(event) {
            className = $(this).attr('class');
          });

          $('#test').on('click, function(event) {
        Drupal.myTest.myFunction();
          });

    }

  };

}(jQuery));
4

2 回答 2

1

尝试

(function ($) {
    var className;
    var flavor;

    Drupal.myTest = Drupal.myTest|| {};

    Drupal.behaviors.myTest = {
        attach: function (context, settings) {
            Drupal.myTest.myFunction= function (className) {
                var passedVar= className;
                alert(passedVar);
            };

            $('.buttons').on('click', function(event) {
                className = $(this).attr('class');
            });

            $('#test').on('click', function(event) {
                Drupal.myTest.myFunction(className);
            });
        }
    };

}(jQuery));
于 2013-08-23T15:42:34.820 回答
0

在外部声明函数$(document).ready()

<script type="text/javascript">
    var myPassedVar;

    $(document).ready(function() {
        $('.buttons).click(function() {
          var className = $(this).attr(class);
        });

        myFunction = function (className) {
          myPassedVar = className;
          alert(myPassedVar);
        };

        $('#test').click(function() {
          myFunction(className);
        }
    }); //END $(document).ready()
</script>
于 2013-08-23T15:33:43.827 回答