简体   繁体   English

使用Ajax传递表单数据

[英]Passing form data with Ajax

I have a page which opens a thickbox of another page which contains a form within it. 我有一个页面,可以打开另一个页面的thickbox,其中包含一个表单。 However, upon that form being submitted (it writes the data to the DB) I need the parent page of the thickbox to update certain rows of the form (the values that have been changed). 但是,在提交该表单后(它将数据写入数据库),我需要thickbox的父页面来更新表单的某些行(已更改的值)。

I have been reading online, but I have never really attempted doing anything like this with Ajax before (i just normally use jQuery load()). 我一直在网上阅读,但之前我从未真正尝试过使用Ajax做类似的事情(我通常只使用jQuery load())。 Essentially my theory is that I could use the jQuery .submit() funciton and have a callback function which takes the post values and passes them to the previous page. 本质上,我的理论是我可以使用jQuery .submit()函数,并具有一个回调函数,该函数接受发布值并将其传递到上一页。 I am unsure however to pass the values from the form caught in the callback function to the previous page as the form although being shown on the page in a thickbox is a different page. 我不确定将值从回调函数中捕获的表单传递到上一页,尽管该表单显示在thickbox中的页面是另一页。

Any help would be greatly appreciated! 任何帮助将不胜感激!

Just to help visulaise what the page looks like: 只是为了帮助说明页面的外观: 在此处输入图片说明

The form showing details is under the thickbox, and once the update buttons is clicked I would like to have a way of passing the new details back to that form without having to refresh the entire page. 显示详细信息的表单位于thickbox下,单击更新按钮后,我希望有一种方法可以将新的详细信息传递回该表单,而不必刷新整个页面。

Many thanks, 非常感谢,

Well there are several ways you could do it one could be to find the div that the thickbox opens then find the iframe in there which would in theory show the returned data from the server depending on how you are returning it and then using jQuery you could extract the data and update the original page. 好吧,有几种方法可以做到,一种可能是找到thickbox打开的div,然后在其中找到iframe,理论上将根据您返回的方式显示从服务器返回的数据,然后使用jQuery,提取数据并更新原始页面。

Another way would be to just retrieve the updated data manually through the main page on the onClose event of the thickbox which IMO is a waste of a call if the iframe returns it. 另一种方法是仅通过thickbox的onClose事件上的主页手动检索更新的数据,如果iframe返回,则IMO浪费了调用。

That all depends on how your server returns the data to the form though. 但这一切都取决于服务器如何将数据返回到表单。

You can do two things, first you might want to use the form submit event to trigger an update of the form on the page: 您可以做两件事,首先,您可能想使用表单提交事件来触发页面上表单的更新:

$('#myThickboxForm').submit(function() {

    // take the data in one of the form fields
    var fieldInputData = $('#someThickboxFormField').val();

    // now update the other form using this value
    $('#pageForm').find('#aPageFormField').val(fieldInputData);

});

Your thickbox also triggers an "unload" event when it gets closes, you can listen to that event like this: 当thickbox关闭时,thickbox还会触发“卸载”事件,您可以像这样监听该事件:

$('#TB_window').on('unload', someFunction);

var someFunction = function() {

    // do something when the thickbox closes

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM