2015-05-22 118 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>