将函数直接传递给change()事件不起作用

时间:2017-06-20 17:06:11

标签: javascript jquery

我必须根据选择的值显示或隐藏一些div。要做到这一点,我有一个很好的代码块:

// board.js
var Board; 

Board = {
    state: [[undefined, undefined, undefined], [undefined, undefined, undefined], [undefined, undefined, undefined]],

    isInWinningState: function() { 
        return Board.topLeftToBottomRightIsComplete() || 
            Board.topRightToBottomLeftIsComplete() || 
            Board.aRowIsComplete() || 
            Board.aColIsComplete();
    }, 

    newMove: function(newMarker, x, y) {
        return Board.state.map( function(row, rowIndx) {
            if (rowIndx===y) {
                return row.map( function (marker, colIndx) {
                    if (colIndx===x) {
                        return newMarker;
                    } else {
                        return marker;
                    }
                });
            } else {
                return row;
            }
        });
    },

};

module.exports = Board;




// player.js
var Player; 

Player = {
    marker: undefined,
    turn : false,
    isWinner: false,

    assignMarker: function(newMarker) {
        this.marker = newMarker;
    }, 

    startTurn: function() {
        this.turn = true;
    },

    move: function() {
        if (this.turn = true) {
            // move
        } else {
            // player can't move
        }
    },

    wins: function() {
        this.isWinner = true;
    }
}

module.exports = Player;




// tic-tac-toe.js
let Board = require('./board.js'),
    Player = require('./player.js');

var TicTacToe;

TicTacToe = {
    board: undefined,
    player1: undefined,
    player2: undefined,
    currentPlayer: undefined,

    startGame: function() {
        this.board = Object.create(Board);
        this.currentPlayer = this.player1.marker==='X' ? this.player1 : this.player2;
    },

    initPlayer1: function() {
        marker = Math.random() > 0.5 ? 'X' : 'O';
        this.player1 = Object.create(Player);
        this.player1.assignMarker(marker);
    },

    initPlayer2: function() {
        marker = this.player1.marker==='X' ? 'O' : 'X';
        this.player2 = Object.create(Player);
        this.player2.assignMarker(marker);
    },

    newTurn: function() {
        this.currentPlayer = this.currentPlayer===this.player1 ? this.player2 : this.player1;
    },

    gameOver: function() {
        if this.board.isInWinningState() {
            return true;
        } else {
            return false;
        }
    }
}

module.exports = TicTacToe;

在底部,当用户更改选择值时,我必须将函数作为参数传递给事件$(document).ready(function($) { function formaCaptacao() { $('.receptivo').parent().hide(); $('.indicacao').parent().hide(); $('.interna').parent().hide(); var a = $('#cp-forma-captacao').val(); console.log(a); switch( $('#cp-forma-captacao').val() ) { case 'contato_receptivo': $('.receptivo').parent().show(); break; case 'indicacao': $('.indicacao').parent().show(); break; default: $('.interna').parent().show(); break; } } formaCaptacao(); $('#cp-forma-captacao').change(function(){ formaCaptacao(); }); }); 。我试过这个:

change()

但它不起作用。就像从未调用$('#cp-forma-captacao').change( formaCaptacao() ); 事件一样。为什么?我只是将函数作为参数传递。从理论上讲,它应该有效。

1 个答案:

答案 0 :(得分:2)

在你的例子中,你正在调用函数而不是将引用传递给函数本身 - 所以你实际传入change()的是调用formaCaptacao的结果(这将是undefined,因为它永远不会返回任何东西),而不是函数本身。

试试这个......

$('#cp-forma-captacao').change(formaCaptacao);

如果你认为它就像你在做这个......

var formCaptaco = function() { console.log('example!'); }
$('#selector').change(formCaptacao);

......它可能更有意义(它实际上比实际上稍微复杂一些,请参阅https://rachel53461.wordpress.com/2011/05/28/switching-between-viewsusercontrols-using-mvvm/以了解差异的描述)。