在表单中动态添加输入字段

获得服务端的值,将name="sites[]" 添加到现有的文本框,则可以通过$_POST['sites]访问数组
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
fieldset{width:450px;}
ul{padding:2px;list-style:none;}
label{float:left;width:100px;}
</style>
</head>
<body>
<form action="process.php" method="post">
<fieldset>
<legend>Websites yo visit dialy</legend>
<ul id="sites">
<li><label>Name</label><input type="text" value="" />
</li>
</ul>
<input type="button" id="add" value="Add More" />
</fieldset>
</form>

<script type="text/javascript" src="../jquery-1.4.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
     $('#add').click(function(){
          var str='<li>';
          str+='<label>Name</label><input type="text" />';
          str+='<input type="button" value="remove" class="remove" />';
          str+='</li>';
          $('#sites').append(str);
          $('.remove').live('click',function(){
               $(this).parent('li').remove();
          });
     });
    
    
});

</script>
</body>
</html>
posted @ 2014-04-04 17:26  wint  Views(470)  Comments(0)    收藏  举报