-1

我需要设置一个事件处理程序,其工作是返回正在设置处理程序的函数,而不是匿名触发器处理程序本身。如何做到这一点?请看下面的代码:

if(subFormToServer())
    doSuccessDance();
else
    doErrorStuff();

function subFormToServer(thaGoods) {
  $('<iframe name="subform-target" style="display: none;"></iframe>')
    .bind('load',  function(){ return 0; }) // how to I "pass" these return statements "up" to my function or "escape" them, so to speak, so that what they're returning isn't the load/error handler functions, but actually the subFormToServer function?
    .bind('error', function(){ return 1; })
    .appendTo('body');

 $('<form method="POST" '+
          'action="http://server/formHandler.php" '+
          'target="subform-target"> '+

        '<input name=inputName              value="'+encodeURIComponent(thaGoods)+'">'+

    '</form>')[0]
    .submit();
}
4

1 回答 1

4

那是不可能的。考虑这种情况,如果是:

// Assume form takes 10 seconds to respond
var x = subFormToServer(...)

// form has neither loaded nor errored yet - what does x contain?
alert(x)

你最好的选择是:

subFormToServer(thaGoods, doSuccessDance, doErrorStuff);

function subFormToServer(thaGoods, worked, didnt) {
    $('<iframe name="subform-target" style="display: none;"></iframe>')
        .on('load',  worked)
        .on('error', didnt)
        .appendTo('body');

    $('<form />', {
        method: "POST",
        action: "http://server/formHandler.php",
        target: "subform-target"
    }).append(
        $('<input />', {
            name: "inputName",
            value: encodeURIComponent(thaGoods)
        })
    ).submit()
}
于 2012-08-19T09:14:54.800 回答