【问题标题】:Extend Forms with Javascript使用 Javascript 扩展表单
【发布时间】:2011-04-26 12:23:33
【问题描述】:

http://www.quirksmode.org/dom/domform.html

我正在阅读上面提到的网站并尝试在我的项目中实施它。但是,当我单击“获取更多字段”按钮时,它就不起作用了。这是代码:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>Test Form</title>
        <script type="text/javascript">

            var counter = 0;

            function moreFields() {
                counter++;
                var newFields = document.getElementById("readroot").cloneNode(true);
                newFields.id = '';
                newFields.style.display = 'block';
                var newField = newFields.childNodes;
                for (var i=0;i<newField.length;i++) {
                    var theName = newField[i].name;
                    if (theName)
                        newField[i].name = theName + counter;
                }
                var insertHere = document.getElementById("writeroot");
                insertHere.parentNode.insertBefore(newFields,insertHere);
            }

            window.onload = moreFields;


        </script>
    </head>
    <body>
        <div id="readroot" style="display: none">

            <input type="button" value="Remove review"
                   onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /><br /><br />

            <input name="cd" value="title" />

            <select name="rankingsel">
                <option>Rating</option>
                <option value="excellent">Excellent</option>
                <option value="good">Good</option>
                <option value="ok">OK</option>
                <option value="poor">Poor</option>
                <option value="bad">Bad</option>
            </select><br /><br />

            <textarea rows="5" cols="20" name="review">Short review</textarea>


        </div>

        <form method="post" action="/cgi-bin/show_params.cgi">

            <span id="writeroot"></span>

            <input type="button" id="moreFields" value="Give me more fields!" />
            <input type="submit" value="Send form" />

        </form>
    </body>
</html>

【问题讨论】:

  • 很抱歉,请问您如何从创建的表单中读取变量?最后你有 vaible1,variable2,variabl3.... 你如何读取它们并将它们传递给 SQL?

标签: javascript dom forms extend


【解决方案1】:

我解决了。在按钮代码下,我添加了onclick="moreFields()" 并删除了id,瞧!它就像一个魅力。

【讨论】:

    【解决方案2】:

    添加 onclick="moreFields()" 并删除按钮 ID。

    它会正常工作的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-03
      • 1970-01-01
      • 2018-10-09
      • 2013-10-21
      相关资源
      最近更新 更多