我正在使用PHP和JQuery创建一个网站,并进入问题rearding形式。不能理解为什么我的网站保持导航到其索引页?
例如,我的index.php如下所示:
<body>
<?php
echo "<div id=\"web_Page\">";
require("public/templates/header.php");
require("public/templates/menu.php");
require("public/templates/home.php");
require("public/templates/footer.php");
echo "</div>";
?>
</body>
它然后加载页眉,页脚,菜单区和起始页。使用JavaScript代码
<div id="web_Page">
<div id="web_Header">
//contents of header.php loaded into this div//
</div>
<div id="web_Menu">
//contents of menu.php loaded into this div//
</div>
<div id="web_Contents">
//contents of home.php loaded into this div//
</div>
<div id="web_Footer">
//contents of footer.php loaded into this div//
</div>
</div>
菜单项,然后装入web_contents DIV区域:
$('#web_Menu a').click(function(e)
{
e.preventDefault();
$('#web_Content').load($(this).attr('href'), function()
{
});
});
已加载的文件也被包围在DIV区域,以便最终的index.php呈现像这样
现在,代码将从菜单中选择的所有页面加载到web_Contents div区域。但是我现在创建了一个名为register.php的页面,它有一些非常意想不到的功能。下面是它呈现的HTML,它也将在web_Contents加载DIV面积:
<div id="reg_Div">
<form id="reg_Form>
<label>Desired Username: </label><input type="text" name="uname" id="uname" />
<button>Register</button>
</form>
</div>
index.php页面将被呈现:
<div id="web_Page">
<div id="web_Header">
//contents of header.php loaded into this div//
</div>
<div id="web_Menu">
//contents of menu.php loaded into this div//
</div>
<div id="web_Contents">
<div id="reg_Div">
<form id="reg_Form>
<label>Desired Username: </label><input type="text" name="uname" id="uname" />
<button>Register</button>
</form>
</div>
</div>
<div id="web_Footer">
//contents of footer.php loaded into this div//
</div>
</div>
的问题是,即使这个表单没有功能,按钮也没有,只要点击按钮,网站导航到index.php页面。我真的不明白为什么?
任何人都可以看到为什么代码会导致触发按钮导航到index.php。另外,由于index.php只是require()页面的集合,并且register.php也在那里加载,导致页面自动丢弃包含register.php的web_Contents div并将其替换为home的内容。 PHP的?
这真的让我困惑?
任何反馈将不胜感激。 谢谢。
你可以把听者在提交事件太...: '$( 'reg_Form')提交(函数(){ e.preventDefault();返回false; });'。 – 2011-04-11 17:30:51