我想在表单中执行onsubmit后调用另一个函数

时间:2011-10-22 09:50:55

标签: javascript html

请给我一些建议,我试图运行以下代码:

< form name="form1" action="some_action" method="post" onsubmit="somefunction(); return false;">

.... some code...

< input type="submit" value="GET">

< /form>

在这里,我想运行onsubmit(运行良好且正如预期的那样给出一些对话框作为输出,工作正常)

然后我想运行未运行的动作(因为在onsubmit中返回false)但我想以任何方式运行它。

< form action="login/buy_artifact/0/<?=$row['A_ID']?>/<?=$userid?>" onSubmit="return btn(<?=$i?>,<?=$row['A_ID']?>,<?=$userid?>);">

< input type="submit" name="submit" value=" GET ">
< /form>

function btn(id,art_id,user_id)
    {   
    //alert("called"+id);
      var id=id;
      var name = document.getElementById("name"+id).innerHTML; 
      var desc = document.getElementById("description"+id).innerHTML;
      var credit = document.getElementById("creditsforquestion"+id).innerHTML;

      var art_id=art_id;
      var user_id=user_id;

      // Only send param data for sample. These parameters should be set
      // in the callback.
      var order_info = { "name":name,
                         "description":desc,
                         "credit":credit,
                         "aid":art_id,
                         "uid":user_id
                       };

      // calling the API ...
      var obj = {
        method: 'pay',
        order_info: order_info,
        purchase_type: 'item'
      };

      FB.ui(obj, callback);     
    var callback = function(data) {

      if (data['order_id']) {

        writeback("Transaction Completed! </br></br>"
        + "Data returned from Facebook: </br>"
        + "<b>Order ID: </b>" + data['order_id'] + "</br>"
        + "<b>Status: </b>" + data['status']);

      } else if (data['error_code']) {

        writeback("Transaction Failed! </br></br>"
        + "Error message returned from Facebook:</br>"
        + data['error_message']);
        return false;
      } else {

        writeback("Transaction failed!");
      }

    };

    }

3 个答案:

答案 0 :(得分:1)

你可以这样做:

<form name="form1" action="some_action" method="post" onsubmit="return somefunction();">
    .... some code...
    <input type="submit" value="GET" />
</form>

然后在你的somefunction中根据某些条件决定是返回true还是false:

function somefunction() {
    if (....) {
        // do something and return true in order to submit the form
        return true;
    } else {
        // do not submit the form
        return false;
    }
}

答案 1 :(得分:0)

删除return false;来自onsubmit

答案 2 :(得分:0)

在表格标签中使用

onsubmit="return somefunction();"

并且在javascript函数中如果没有满足任何条件则返回false,否则在方法中最后返回true;