2

フォーム要素のぼかしでいくつかのことをしようとしています。私が抱えている問題は、ID、クラスなどの要素の情報を 2 番目の関数に渡すことです。この例では、単純化しました。

function otherfunction() {
    var inputID = $(this).attr("id");
    alert(inputID);
}


$(".formelement").blur(function () { 

// Do some stuff here

otherfunction();

}); 

もちろん、アラート ボックスには、inputID が定義されていないと表示されます。要素の情報を他の関数に渡すにはどうすればよいですか?

4

3 に答える 3

3

入力を引数として渡します。

function otherfunction(el) {
    var inputID = $(el).attr("id");
    alert(inputID);
}


$(".formelement").blur(function () {
    // Do some stuff here

    otherfunction(this);
}); 

または、次を使用しますFunction.prototype.apply

function otherfunction() {
    var inputID = $(this).attr("id");
    alert(inputID);
}


$(".formelement").blur(function () {
    // Do some stuff here

    otherfunction.apply(this);
}); 
于 2012-04-11T19:09:55.990 に答える
2

次の場合は$.proxyを使用します。

$(".formelement").blur($.proxy(otherfunction, this));

それ以外の場合は、javascript のcallまたはapply :

$(".formelement").blur(function () { 

    // Do some stuff here

    otherfunction.call(this); // or otherfunction.apply(this); 
});
于 2012-04-11T19:11:40.173 に答える
0

私はあなたがこのように使うことができると思います:

function otherfunction(obj) {
    var inputID = $(obj).attr("id");
    alert(inputID); }


$(".formelement").blur(function () { 

otherfunction($(this));

});
于 2012-04-11T19:15:10.983 に答える