2015-05-22 69 views
0

我有一个页面,我在输入中输入文本,它是一个简单的形式。我有一个按钮,当我点击按钮时,是否可以打开另一个页面并通过jquery将数据发布到这个新窗口。我不是在这里讨论ajax,因为我想打开一个新窗口并转到持有帖子表单的新页面。在新的打开的窗口jquery post

所以我有第1页与外形和输入1.我在输入1进入测试,然后如果我点击提交,它会在那里有一个

$_POST['temp'] 

打开一个新的PHP页面这将是填入以便代码自动执行。事实上,这是为了避免在网址中输入所有内容并转到该网址。

看来我还不够清楚。我知道如何用表单和提交按钮来完成它。但我想通过jquery提交表单,所以当我点击一个按钮或其他将被我的javascript拦截的其他内容时,它将查找输入,然后重定向到其他页面并发布我想要的数据。

+1

呃,这是什么形式呢?如果你想在某个地方重定向,你可以设置表单的'action'属性? – adeneo

+0

http://www.w3schools.com/tags/att_form_action.asp - 查看“尝试自己”的代码以了解操作属性的工作原理。希望它有帮助。 – Falt4rm

+0

我说使用jquery时! – Richard

回答

1

如果我理解正确,您想从父级打开的子窗口(弹出窗口)访问父窗口表单输入值。 如果是这样,我建议你看一下window.opener财产 http://www.w3schools.com/jsref/prop_win_opener.asp

如果你有这样的父窗口的情况:

HTML:

<form id="testForm"> 
    <input type="text" id="testVariable" value="" /> 
    <input type="submit" /> 
</form> 

JS:

$(document).ready(function() { 
    $('#testForm').on('submit', function(e){ 
     e.preventDefault(); 
     window.open('http://www.yourdomain.com/chilwindow.php', 'ChildWindow', 'menubar=0,resizable=1,location=0,scrollbars=0,status=0,toolbar=0,width=580,height=520'); 
    }); 
}); 

然后在你的子窗口/弹出窗口(http://www.yourdomain.com/chilwindow.php),你应该可以访问你的父风流量是这样的:

$(document).ready(function() { 
    var testVariableNew = window.opener.$("#testVariable").val(); 
    console.log('Got value from parent window: ' + testVariableNew); 
}); 

或者:

$(document).ready(function() { 
    var testVariableNew = $('#testVariable', window.opener.document).val(); 
    console.log('Got value from parent window: ' + testVariableNew); 
}); 

如果你想要做一个POST请求执行以下操作:

$("#testForm").submit(function(e) { 
    e.preventDefault(); 
    var myVariable = $(this).find("#testVariable").val(); 
    var myUrl = $(this).attr("action"); 
    var posting = $.post(myUrl , { temp: myVariable }); 
    posting.done(function(result) { 
    console.log(result); 
    }); 
}); 
0

的jquery.form.js插件不正是你想。 http://malsup.com/jquery/form/

<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 

    <script> 
     // wait for the DOM to be loaded 
     $(document).ready(function() { 
      // bind 'myForm' and provide a simple callback function 
      $('#myForm').ajaxForm(function(response) { 
       alert("The script said: "+response); 
      }); 
     }); 
    </script> 
</head>