写form提交应该注意的
前两天改bug,发现一个表单提交提交的问题,先上代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Test</title> <script type="text/javascript" src="http://www.z7zba.com/js/jquery-1.4.4.min.js"></script></head> <style type="text/css"> </style> <body> <?print_r($_POST); echo "\r\n";?><table border="0" cellpadding="0" cellspacing="0"><form id="theFrom" name="theForm" action="demo.php" method="post"><tr> <td id="tdName"><input type="text" id="name" name="name" /></td> </tr><tr> <td><a href="javascript:searchName();">查找姓名</a> <a href="javascript:doSubmit();">提交</a></td> </tr> </form></table><script type="text/javascript"> function searchName(){//可以是ajax返回内容var data = '<select id="nameId" name="nameId"><option value="1">Cheech</option><option value="2">Mark</option></select>';$("#tdName").html(data);}function doSubmit(){$("#theFrom").submit();}</script> </body> </html>当其中的 <input type="text" id="name" name="name" /> 被换成 <select id="nameId" name="nameId"><option value="1">Cheech</option><option value="2">Mark</option></select> 进行提交。
$_POST['nameId']在使用 ie 浏览器提交,程序可正常接收到参数 ,而使用Chrome,FireFox提交,程序不能提收到参数
问题出在form的位置套错了。将form套在table外问题就解决了。如下:(其他不变)
<form id="theFrom" name="theForm" action="demo.php" method="post"><table border="0" cellpadding="0" cellspacing="0"><tr> <td id="tdName"><input type="text" id="name" name="name" /></td> </tr><tr> <td><a href="javascript:searchName();">查找姓名</a> <a href="javascript:doSubmit();">提交</a></td> </tr></table></form>