如何使用css和js制作动态html表单

时间:2015-10-11 09:34:05

标签: javascript html css

为什么我的表单在点击时不添加字段。我错过了什么吗?  我想在点击添加另一个字段按钮时添加文件,但它无法正常工作。



<script type="text/javascript">


$(function() {
        var scntDiv = $('#p_scents');
        var i = $('#p_scents p').size() + 1;

        $('#addScnt').live('click', function() {
$('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
                i++;
                return false;
        });

        $('#remScnt').live('click', function() {
                if( i > 2 ) {
                        $(this).parents('p').remove();
                        i--;
                }
                return false;
        });
});


</script>
&#13;
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
<style media="screen">
* { font-family:Arial; }
h2 { padding:0 0 5px 5px; }
h2 a { color: #224f99; }
a { color:#999; text-decoration: none; }
a:hover { color:#802727; }
p { padding:0 0 5px 0; }

input {
    padding:5px;
    border:1px solid #999;
    border-radius:4px;
   -moz-border-radius:4px;
   -web-kit-border-radius:4px;
   -khtml-border-radius:4px;
  }
</style>

  </head>
  <body>
    <h2><a href="#" id="addScnt">Add Another Input Box</a></h2>

    <div id="p_scents">
        <p>
            <label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" /></label>
        </p>
    </div>
</body>
</html>
&#13;
&#13;
&#13;

&#13;
&#13;
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
<style media="screen">
* { font-family:Arial; }
h2 { padding:0 0 5px 5px; }
h2 a { color: #224f99; }
a { color:#999; text-decoration: none; }
a:hover { color:#802727; }
p { padding:0 0 5px 0; }

input {
    padding:5px;
    border:1px solid #999;
    border-radius:4px;
   -moz-border-radius:4px;
   -web-kit-border-radius:4px;
   -khtml-border-radius:4px;
  }
</style>

  </head>
  <body>
    <h2><a href="#" id="addScnt">Add Another Input Box</a></h2>

<div id="p_scents">
    <p>
        <label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" /></label>
    </p>
</div>

<script src ="https://code.jquery.com/jquery-1.4.3.min.js"></script>

<script>
$(function() {
        var scntDiv = $('#p_scents');
        var i = $('#p_scents p').size() + 1;

        $('#addScnt').live('click', function() {
$('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" class="remScnt">Remove</a></p>').appendTo(scntDiv);
                i++;
                return false;
        });

        $(document).live('click', '.remScnt',function() {

                        $(this).parents('p').remove();

                return false;
        });
});
</script>

</body>
</html>
&#13;
&#13;
&#13;

我从http://jsfiddle.net/jaredwilli/tzpg4/4/

复制了此内容

2 个答案:

答案 0 :(得分:0)

你确定你加入了jQuery吗? (我在你的片段中没有看到它)当我点击你提供的小提琴中的'添加另一个输入框'时,我得到另一个输入框。包括<script src ="https://code.jquery.com/jquery-1.4.3.min.js"></script>在头部。包括版本1.4.3,因为它使用live而不是on

答案 1 :(得分:0)

没有jquery,你的脚本包含在你的html脚本中。 jquery文件用作外部javascript文件,因此可以先添加jquery文件。 注意:您必须在使用jquery的脚本之前包含jquery。 了解外部javascript here