根据用户输入返回值

时间:2012-02-08 14:51:44

标签: javascript jquery

使用jQuery(1.7),而不是jQueryUI - (如何)可以完成以下任务?

  • 通话功能1
  • 在函数1中,定义一个变量并设置为 功能2的结果
  • 当被调用时,功能2'即时'会创建2个按钮,如果 单击第一个返回值'true',第二个返回'false'
  • 在函数1中,设置为函数2结果的变量应该等到用户输入并且返回了值
  • 然后应评估此值

有没有办法在jQuery中执行此操作而不使用间隔/超时等来继续查找设置为用户输入的中间值?

我概述了以下原则:

function function1(){

    var myVariable=function2;
    // nothing should happen at this point until function2 has returned a response
    if(myVariable){
        alert('true');
    }else{
        alert('false');
    }
}


function function2(){

    var yesbutton=$("<input type='submit' value='yes' />");
    var nobutton=$("<input type='submit' value='no' />");
    $('body').append(yesbutton)
    $('body').append(nobutton)
    nobutton.click(function(){
        return false;
    });
    yesbutton.click(function(){
        return true;
    });

}


function1();

一如既往,非常感谢您的帮助!

4 个答案:

答案 0 :(得分:4)

有可能,可能不会像你期望的那样。

function function1(){

    var myVariable=function2();
    // nothing should happen at this point until function2 has returned a response
    myVariable.done(function(){
        alert('true');
    }).fail(function(){
        alert('false');
    });
}


function function2(){
    var deferred = $.Deferred();

    var yesbutton=$("<input type='submit' value='yes' />");
    var nobutton=$("<input type='submit' value='no' />");
    $('body').append(yesbutton)
    $('body').append(nobutton)
    nobutton.click(deferred.reject);
    yesbutton.click(deferred.resolve);

    return deferred.promise().always(function(){
        yesbutton.add(nobutton).remove();
    });

}


function1();

答案 1 :(得分:3)

你可以在回调的帮助下实现它。然而,在一个理想的世界中,最好利用一个事件机制来监听和触发事件以更新变量,但这需要为这个简单的例子付出更多的努力。

function function1(){
    function2(function(myVariable){
        if(myVariable){
            alert('true');
        }else{
            alert('false');
        }
    };
}


function function2(callback){

    var yesbutton=$("<input type='submit' value='yes' />");
    var nobutton=$("<input type='submit' value='no' />");
    $('body').append(yesbutton)
    $('body').append(nobutton)
    nobutton.click(function(){
        callback(false);
    });
    yesbutton.click(function(){
        callback(true);
    });
}

function1();

答案 2 :(得分:2)

你不能这样做,因为function2已经执行,click内的function2处理程序的返回值不是它的返回值。但是你可以只使用一个函数来实现它。

function function1(){

    var yesbutton=$("<input type='submit' value='yes' />");
    var nobutton=$("<input type='submit' value='no' />");
    $('body').append(yesbutton)
    $('body').append(nobutton)
    nobutton.click(function(){
        alert('false');
    });
    yesbutton.click(function(){
        alert('true');
    });

}

或者你也可以这样做。

function function2(yesClickHandler, noClickHandler){
    var deferred = $.Deferred();

    var yesbutton=$("<input type='submit' value='yes' />");
    var nobutton=$("<input type='submit' value='no' />");
    $('body').append(yesbutton).append(nobutton);

    nobutton.click(noClickHandler);
    yesbutton.click(yesClickHandler);

    return deferred.promise();

}

function2(function(){
   alert('true');
}, function(){
   alert('false');
});

答案 3 :(得分:0)

在功能1中使用它:

var myVariable=function2();
相关问题