我正在开发一个消息系统。我已完成收件箱和消息查看部分。它们被加载到用户帐户页面上的div中,并且它在刷新页面时都可以工作。jquery通过表单提交div加载中继内容div
我基于这个在this article上的jquery。
在这里获得一些帮助,以获取消息链接,以便在无需刷新页面的情况下在div内打开消息,以及如何添加筛选器以允许配置文件链接实际刷新并转到实际配置文件页面。这很好。
我转移到使用模式(twitter引导程序)加载外部窗体的消息发送端。这也是有效的,但我现在花了很多时间试图弄清楚如何做与链接相同的事情,使用表单提交,即让它在不刷新整个页面的情况下提交。同样,我正在使用与收件箱部分几乎相同的jQuery。
这里是收件箱中的代码:
<p id="waiting"><!-- ajax loading --></p>
<div class="messages"><!-- inbox --></div>
的Javascript:
$.ajaxSetup({ cache: false});
$(document).ready(function(){
// set up the click event
$('a.loader').live('click', function() {
var toLoad = '<? echo base_url(); ?>user/messaging/';
$('.messages').fadeOut(100, loadContent);
$('#load').remove();
$('#waiting').append('<div id="load" style="height: 700px; background:url(<? echo base_url(); ?>files/recordCovers/index.html?<? echo rand(5,1555); ?>); background-size: cover;"><div class="circle"></div><div class="circle1"></div></div>');
$('#load').fadeOut(1000);
function loadContent() {
$('.messages').load(toLoad, '', function(response, status, xhr) {
if (status == 'error') {
var msg = "Sorry but there was an error: ";
$(".messages").html(msg + xhr.status + " " + xhr.statusText);
}
}).fadeIn(4000, hideLoader());
}
function hideLoader() {
$('#load').fadeOut(2000);
}
return false;
});
});
<? // this makes the links open in the same div
// a:not(.profile) allows profile links to open in browser window
// we put class="profile" on profile links.?>
$(".messages").on("click", "a:not(.profile)", function (e) {
$(".messages").load($(this).attr("href"));
e.preventDefault();
});
好的,对不起,我有点困惑(或者愚蠢)......上面的内容会以包含的形式出现?而不是我从中加载的页面。我用id =“button”按钮替换提交按钮?周围的
如果您需要提交表单,您可能会有表单标签,但您不会提交此表单,只会使用jquery获取数据并使用它。 上面的代码将提交表单白色jquery和AJAX(并且不会刷新页面),如果您使用的是PHP,只需将“要提交的URL”替换为函数路径,请使用PRINT将值返回给jquery。 但是,如果您只需要使用ajax加载表单,请使用: http://api.jquery。com/jQuery.get/ – Theo 2013-03-04 22:34:26