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

标签 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>
<!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>

<!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>

我从 http://jsfiddle.net/jaredwilli/tzpg4/4/ 复制了这个

最佳答案

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

关于javascript - 如何使用 css 和 js 制作动态 html 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33063499/

相关文章:

javascript - 如何使用下面的示例将文本添加到 D3 中的 SVG 元素

javascript - 适合 TH 单元格内的选择字段

css - 扩展水平 DIV 以选择语言

css - 减少 HTML 表格对齐之间的空间

javascript - 我如何从这些数据中获取对象数组以获取状态并在 reactjs 中计数

javascript - 如何从扩展程序检测 Google Chrome 中的页面标题更改?

jquery - 在 Jquery 之上叠加 CSS 元素。 Z指数?

html - 需要一些帮助来定位标题和子标题

javascript - 解析错误 [{{}}] 或使用清理不安全的网址

javascript - 为 Excel 编写一个新函数