将表单提交到另一页

时间:2012-07-02 21:37:35

标签: javascript jquery forms

我需要帮助将数据从#send_form提交到另一页的#f_from。

<script type="text/javascript">
function post_form() {
    $('#send_form').action = "form01.html";
    $('#send_form').submit();
    return false;    
}
</script>

<form id='send_form' action='form01.html' method='POST' onsubmit="post_form();">
<input type="text" name="f_in01" value="User" />
<input type="text" name="f_in02" value="12345" />
<input type="submit" />
</form>

form01.html

<script type="text/javascript">
function f_res()
{
    var res01=document.f_form.f_in01.value;
    var res02=document.f_form.f_in02.value;
    var result = res01 + " " + res02;
    document.f_form.f_out01.value=result;
}
</script>

<form id="f_form" onSubmit="f_res();return false;">
<input type="text" name="f_in01" /><br>
<input type="text" name="f_in02" /><br>
<br>
<input type="submit" onClick="f_res();" value="Enter w/Sub" /><br>
<input type="text" name="f_out01" />
</form>

现在它不起作用。该数据不会在page01.html中发布

3 个答案:

答案 0 :(得分:0)

你试过吗

$('#send_form').attr('action', "form01.html");

而不是

$('#send_form').action = "form01.html";

结帐this fiddle

答案 1 :(得分:0)

Amin是对的,但在jQuery中,当事件处理程序返回false时,它与e.preventDefault(); e.stopPropagation()相同。基本上,您取消该活动。尝试返回true,而不是false。

如果您不想更改页面,则必须查看AJAX以发布表单数据。

答案 2 :(得分:0)

你想要AJAX这样的东西:

    $('#button').click(function(){
    $.ajax({
    type:"POST", //php method
    url:'process.php',//where to send data...
    cache:'false',//IE FIX
    data: data, //what will data contain (no SHIT Sherloc...)

    //check is data sent successfuly to process.php
    //success:function(response){
    //alert(response)
    //}

    success: function(){ //on success do something...

    $('.success').delay(2000).fadeIn(1000);

    //alert('THX for your mail!');
    } //end sucess 
    }).error(function(){ //if sucess FAILS!!
    alert('An error occured!!');
    $('.thx').hide();
    });
    });