首页 > 后端开发 > php教程 > PHPJquery点击按钮添加文本框

PHPJquery点击按钮添加文本框

巴扎黑
发布: 2016-11-11 13:59:56
原创
1979 人浏览过

html页面

Html代码  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

<!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>Jquery添加输入框</title> 

<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script> 

<script type="text/javascript" src="js/jquery.json.min.js"></script> 

</head> 

   

<body> 

    <div id="input_list"

    </div> 

    <input type="button" value="添加" onclick="addInput()"/> 

    <input type="button" value="确认" onclick="isOk()"/> 

       

    <hr> 

    <form action="input.php" method="post"

     <input type="text" id="json_str" name="json" size="100"/> 

     <input type="submit" value="提交" /> 

    </form> 

<script type="text/javascript"

    var index = 1; 

   

    function addInput(){ 

        $("#input_list").append("<input class=&#39;keys&#39; id=&#39;key"+index+"&#39; size=&#39;15&#39; /> : <input class=&#39;values&#39; id=&#39;value"+index+"&#39; size=&#39;50&#39;/> <br>"); 

        index++; 

    

       

    function isOk(){ 

        var len = $(".keys").length;         

           

        var jsonArray = new Array(); 

        for(var i = 1; i<=len; i++){ 

               

            var jsonObj = new Array(); 

               

            var key = $("#key"+i).val(); 

            var value = $("#value"+i).val(); 

            jsonObj[0]=key; 

            jsonObj[1]=value; 

               

            jsonArray.push(jsonObj); 

               

        

           

           

        var jsonstr=&#39;[&#39; 

        for ( var j = 0; j < jsonArray.length; j++) { 

            jsonstr+= &#39;{&#39;;  

            jsonstr+=&#39;\"&#39;+jsonArray[j][0]+&#39;\"&#39;+":"; 

            jsonstr+=&#39;\"&#39;+jsonArray[j][1]+&#39;\"&#39;; 

            jsonstr +=&#39;}&#39; 

            if(j!=jsonArray.length-1){ 

            jsonstr+=&#39;,&#39; 

            

        

        jsonstr+=&#39;]&#39;; 

           

        $("#json_str").val(jsonstr); 

   

        alert("添加成功!"); 

           

    

</script> 

</body> 

</html>

登录后复制

PHP页面

Php代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

<?php 

   

$json = $_REQUEST["json"]; 

$arr=json_decode($json,true); 

?> 

   

<table style="border:1px solid #CCC; width:100%;  background-color:#CCC;"

   

<?php 

foreach($arr as $item){ 

    foreach($item as $key=>$value){ 

?> 

<tr> 

    <td style="width:20%;height:30px; background-color:#E5E5E5;"><?php echo $key;?></td> 

    <td style="width:80%;height:30px; background-color:#FFF;"><?php echo $value;?></td> 

</tr> 

<?php     

    

?> 

</table>

登录后复制


相关标签:
php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板