使用 Javascript 扩展表单

发布于 2024-09-27 23:52:19 字数 2583 浏览 8 评论 0原文

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>

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

I was reading through the site as mentioned above and tried to implement it in my project. But it just doesn't work when i click on "get me more fields" button. Here is the code:

<%@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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情域 2024-10-04 23:52:19

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

I solved it. Under the button code, I added onclick="moreFields()" and deleted the id and voila! It works like a charm.

冰雪之触 2024-10-04 23:52:19

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

它会工作得很好。

Add onclick="moreFields()" and Delete the Button ID.

It will work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文