javascript - 在 HTML 中动态添加表单

标签 javascript html forms

我正在尝试在sharing.html中编写此表单的代码,以便当人们输入完信息并想要添加更多联系人时,他们可以单击“添加另一个联系人”,但是当我尝试写出代码时,我使用的 Dreamweaver 说 Javascript 有错误。我不知道如何修复这个错误:(脚本几乎在底部)

    <div class="recordsbox">
            <h1>Sharing Center</h1>
            <p>Please enter the contact information that you want to share.</p>
            <div id="shareform">
            <form id="share" method="POST">
                 <div class="notifyfield">
                <label>Who is this person?</label>
                <input type="text">
                </div>
                <div class="notifyfield">
                <label>Email Address:</label>
                <input type="email" >
                </div>
                <div class="notifyfield">
                <label>Phone Number:</label>
                <input type="tel" >
                </div>
            </form>
            </div>
            <div class="addcontact">
                <input type="button" value="Add another contact?" onClick="addForm('shareform');">
            </div>
            <script>
            var shareform = 0; 
                function addForm(divName) {
                    shareform++;
                    var newform = document.createElement('div');
                    newform.innerHTML = '<div id="shareform'+shareform+'">
            <form id="share" method="POST">
                 <div class="notifyfield">
                <label>Who is this person?</label>
                <input type="text">
                </div>
                <div class="notifyfield">
                <label>Email Address:</label>
                <input type="email" >
                </div>
                <div class="notifyfield">
                <label>Phone Number:</label>
                <input type="tel" >
                </div>
            </form>
            </div>
';
                    document.getElementById(divName).appendChild(newform);
                    shareform++;
                }
            </script>
            <div class="nextbutton">
            <a href="#">Next</a>
            </div>
        </div>

我根据该教程编写了此代码:http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/

您还可以在 http://hamzakhan.name/dev/eric/options_innerpage/sharing.html 查看完整代码

为了更快地查看,这里是有问题的脚本:

    <script>
            var shareform = 0; 
                function addForm(divName) {
                    shareform++;
                    var newform = document.createElement('div');
                    newform.innerHTML = '<div id="shareform'+shareform+'">
            <form id="share" method="POST">
                 <div class="notifyfield">
                <label>Who is this person?</label>
                <input type="text">
                </div>
                <div class="notifyfield">
                <label>Email Address:</label>
                <input type="email" >
                </div>
                <div class="notifyfield">
                <label>Phone Number:</label>
                <input type="tel" >
                </div>
            </form>
            </div>
';
                    document.getElementById(divName).appendChild(newform);
                    shareform++;
                }
            </script>

最佳答案

这是新代码:

var shareform = 0; 
function addForm(divName) {
  shareform++;
  var newform = document.createElement('div');
  newform.innerHTML = '<div id="shareform'+shareform+'"><form id="share" method="POST"><div class="notifyfield2"><div class="sharefield"><label>Who is this person?</label><input type="text"></div></div><div class="notifyfield"><label>Email Address:</label><input type="email" ></div><div class="notifyfield"><label>Phone Number:</label><input type="tel" ></div></form><hr class="greenline"></div>';
  document.getElementById(divName).appendChild(newform);
  shareform++;
}

您需要删除所有不可见的字符。你不能“换行”字符串。

关于javascript - 在 HTML 中动态添加表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30493380/

相关文章:

javascript - 浏览器大小(宽度和高度)

javascript - GoJS makeSVG 未生成正确的 HTML SVG 对象

html - 如何使 div 堆栈没有空格并保持较小尺寸的顺序 - 在 Bootstrap 中

javascript - 如何获得淡入淡出的模态效果?

javascript - 为什么字符编码对 URL 很重要?

javascript - 绑定(bind)数据列表后,将 css 样式重新应用于 View 的一部分

html - HTML 表单中的 get URL 是如何生成的?

jquery - 提交按钮不会触发 IE 上的 jQuery 绑定(bind)提交事件

javascript - 是否可以将 FormData 应用于现有表单?

javascript - onchange函数被触发两次