0

可能重复:
jQuery/JavaScript “this” 指针混淆

var timeOut = 1000;

$('#'+form +' :input').each(function() {

    thisObj = "";
    if ($(this).parent().parent().find(".incorrect").css('display') == "inline-block" || $(this).attr('value') == "") {
        if ($(this).attr('id') != "submitbtn") {
            thisObj = $(this);
            submit_this++;
            colourfading(this, 'rgba(56, 183, 0, 1)', 'rgba(255, 30, 0, .2)', 'rgb(210,210,210)', 'rgb(250,250,250)');
        }
    }

    var delaySubmit = (function(){
        var timer = 0;
        return function(callback, ms){
            setTimeouts(callback, ms, thisObj);
        };
    })();

    delaySubmit(function(){
        do_sendRequest(ajaxObj, thisObj, true);
    }, timeOut );
    timeOut = timeOut + 1000;
});

这是我在表单上按下提交按钮时使用的一些代码。

您可以看到函数 colourFading 使用“... (this, ' ...”。this 元素是来自 '.each(function() {' 调用的输入元素。

它在我编写的褪色功能中做得很完美,每个输入元素都会变成漂亮的红色,表明它有问题,然后又变回白色。

有趣的来了。如您所见,我已将“thisObj”变量链接为相同的“this”,这将传递给 colourFading,

然而,当我查看 thisObj 的 .attr('id') 时,它表明“this”实际上是我单击的提交按钮的表示。

这很奇怪,因为当我从发送到 colourfading(在 colourfading 函数中)的“this”中询问 id 时,它很好地显示了输入元素的 id。

我试过设置 thisObj = this, thisObj = $(this), 就在 .each(function() { 部分开始之后,就在褪色之前,它似乎有正确的元素,它只是不工作。

我在这里做错了吗?不应该这样工作吗???

4

0 回答 0