2011-04-11 50 views
1

我正在使用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的?

这真的让我困惑?

任何反馈将不胜感激。 谢谢。

回答

1

首先,你在这方面是缺失报价

<form id="reg_Form> // <--- missing a quote 

还,你要更新到这样的事情...

<form id="reg_Form" onsubmit="return(false);"> // <--- should prevent form submittion 
+1

你可以把听者在提交事件太...: '$( 'reg_Form')提交(函数(){ e.preventDefault();返回false; });'。 – 2011-04-11 17:30:51

1

形式有action属性,其告诉浏览器在点击提交按钮后浏览的位置。如果没有指定action属性,那么默认情况下它是当前页面,在你的情况下是“index.php”。

0

HTML表单提交他们的数据到一个页面,如果没有指定,那么它提交到当前页面(index.php在你的情况)。

<form id="reg_Form"> 

相同

<form id="reg_Form" action="index.php" method="GET"> 

要停止提交表单,创建一个onsubmit处理,告诉它不要提交。

$('#reg_Form').submit(function(e){ 
    e.preventDefault(); // Tells the form not to submit 
}); 
0

表单点击按钮,尽管没有一个action属性之后提交的原因,就是action属性默认为当前页面的URL,如果不指定。

阻止提交表单的一种方法是在表单标记中包含onsubmit属性。如果此属性中包含的JavaScript代码返回false,则表单将不会被提交。

<form id="reg_Form" onsubmit="return false;"> 
相关问题