简体   繁体   English

使用ajax加载弹出窗口

[英]Loading a popup using ajax

I have a jsp page which should load a popup using ajax. 我有一个jsp页面,应该使用ajax加载弹出窗口。 The content of the page is determined by form filled by user. 页面内容由用户填写的表单确定。

Something like this: 像这样的东西:

javascript: JavaScript的:

ajax('getPage.action', 'content_id', 'form_id');

foo.jsp: foo.jsp:

<div id="content_id"></div>

<form id="form_id">
 ...
</form>

java/spring: 爪哇/春:

@RequestMapping("getPage.action")
MyController extends AbstractCommandController {
  RealDto dto = (RealDto)command;
  ...
  return new ModelAndView("foo", data);
}

The most difficult part for me is how to send the form data easily as an ajax call. 对我来说最困难的部分是如何轻松地将表单数据作为ajax调用发送。 Can I use jQuery here? 我可以在这里使用jQuery吗? The form changes dynamically so it would be quite bothersome to list all the fields of the form. 表单会动态更改,因此列出表单的所有字段会非常麻烦。

Would it help to use Springs XT (which I never have)? 使用Springs XT(我从未拥有)会有帮助吗?

jQuery form plug-in can help you easily transform a regular form to an Ajax one. jQuery表单插件可以帮助您轻松地将常规表单转换为Ajax表单。 You only need a single line of code: 您只需要一行代码:

$("#myform").ajaxForm(
   {beforeSubmit: validate, success: showPopup} );

Yes, you can use serialize to trivially convert the form to send the data. 是的,您可以使用序列化来简单地转换表单以发送数据。

$("#form1").submit(function() {
    $.get("/desiredURL", $("#form1").serialize(), function(response) {
        // send response data to a popup
    }
}

You can use get or post to send the data. 您可以使用getpost来发送数据。

For the popup I like facebox , but there's loads of choices. 对于弹出窗口我喜欢facebox ,但有很多选择。

I don't know about jQuery, but for prototype this is easy: 我不知道jQuery,但对于原型这很容易:

new Ajax.Request('getPage.action', {
    parameters: $('form_id').serialize(true),
    onSuccess: someMethod
);

Check out the Prototype API docs . 查看Prototype API文档

This page has the same information for jQuery: http://docs.jquery.com/Ajax 该页面包含与jQuery相同的信息: http//docs.jquery.com/Ajax

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

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