-3

要素のIDを取得し、別の子関数で利用したい

function example(id) {
    var me = this;
    this.pro = ID
    this.code = function () {
        setTimeout(function () {
            alert(ID)
        }, 20)
    }
    this.validate = function () {
        $('.' + id).keyup(function (e) {
            var ID = this.id;
            if (e.keyCode == 13) me.code()
        })
    }
}

<input type="text" class="test" id="1" />
<input type="text" class="test1" id="2" />

<script type="text/javascript">
    var test = new example('test')
    var test1 = new example('test1')
    test.validate()
    test1.validate()
</script>
4

2 に答える 2

1

proプロパティを使用するか

function example(id) {
    var me = this;
    this.pro = null;
    this.code = function () {
        setTimeout(function () {
            alert(me.pro);
        }, 20);
    };
    this.validate = function () {
        $('.' + id).keyup(function (e) {
            me.pro = this.id;
            if (e.keyCode == 13) me.code();
        });
    };
}

または、その関数のパラメーターにします。

function example(id) {
    var me = this;
    this.code = function (ID) {
        setTimeout(function () {
            alert(ID);
        }, 20);
    };
    this.validate = function () {
        $('.' + id).keyup(function (e) {
            var ID = this.id;
            if (e.keyCode == 13) me.code(ID);
        });
    };
}
于 2013-07-31T15:14:05.820 に答える