在动态创建的表单中添加和删除输入元素

发布于 2024-09-28 00:25:34 字数 1106 浏览 2 评论 0原文

我有一个“消息”页面,可以在其中对每条消息进行评论。我正在做一个类似 facebook 的文本区域,当你聚焦时它会显示提交按钮,当你 onblur 时它会再次隐藏它。到目前为止,我已经尝试了一些不同的事情:

<script type="text/javascript">
function focusTextarea(id) {
        var form = document.forms[id];

       //Create an input type dynamically.
        var element = document.createElement("input");

        //Assign different attributes to the element.
        element.setAttribute("type", "submit");0
        element.setAttribute("value", "Post comment");
        element.setAttribute("name", "createComment");
        element.setAttribute("class", "okButton");
        element.setAttribute("id", "test");

        var foo = document.getElementById("commentButton");

        //Append the element in page (in span).
        form.appendChild(element);

}

function unFocusTextarea(id) {
    var test = document.getElementById(id);

    var foo = document.getElementById("commentButton");
    var foo2 = document.getElementById("test");

    foo.removeChild(foo2);
}
</script>

参数 id 是表单名称和 id。在第一个函数中,我想找到表单并插入一个提交按钮。第二个功能我想再次找到表单并删除按钮。

提前致谢

I have a "messages" page where it is possible to comment on every message. I'm doing a facebook-like textarea where when you focus it shows the submit button and when you onblur it hides it again. So far i've tried some different things:

<script type="text/javascript">
function focusTextarea(id) {
        var form = document.forms[id];

       //Create an input type dynamically.
        var element = document.createElement("input");

        //Assign different attributes to the element.
        element.setAttribute("type", "submit");0
        element.setAttribute("value", "Post comment");
        element.setAttribute("name", "createComment");
        element.setAttribute("class", "okButton");
        element.setAttribute("id", "test");

        var foo = document.getElementById("commentButton");

        //Append the element in page (in span).
        form.appendChild(element);

}

function unFocusTextarea(id) {
    var test = document.getElementById(id);

    var foo = document.getElementById("commentButton");
    var foo2 = document.getElementById("test");

    foo.removeChild(foo2);
}
</script>

The parameter id is the form name and id. In first function i want to find the form and insert a submit button. The second function i want to again find the form and remove the button.

Thanks in advance

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

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

发布评论

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

评论(3

北渚 2024-10-05 00:25:34

对于您想做的事情,您不应该创建和删除对象,您可以简单地使用 CSS 隐藏对象。当您希望隐藏 DOM 元素及其所有子元素时,将 DOM 元素的 .style.display 属性设置为 "none",并将其设置为 " " 当您希望它们显示时。

至于您当前的代码,我认为 document.forms 并没有达到您的预期。表单不是一个范围,它是一个 DOM 元素,您最好像其他元素一样引用它:document.getElementById("formid")

For what you want to do you shouldn't create and remove objects, you can simply hide an object using CSS. Set the .style.display property of a DOM element to "none" when you want it, and all its child elements, to be hidden, and to "" when you want them to show.

As for you current code, I don't think document.forms does what you think it does. A form is not a scope, it is a DOM element, and you best refer to it like any other: document.getElementById("formid").

雪化雨蝶 2024-10-05 00:25:34

我不明白你想指出什么

document.getElementById("commentButton");

但是,这应该是一个简单的方法:

function unFocusTextarea(id) {
var form = document.getElementById(id);
if(form.lastChild.name=="createComment")form.removeChild(form.lastChild)
}

由于按钮(如果存在)始终是表单的lastChild,因此它只查看这个lastChild并检查名称属性是否匹配给定值(createComment)。如果是这样,该按钮将被删除。

I dont see what you like to point at with

document.getElementById("commentButton");

However, this should be a simple way:

function unFocusTextarea(id) {
var form = document.getElementById(id);
if(form.lastChild.name=="createComment")form.removeChild(form.lastChild)
}

As the button(if he exists) is always the lastChild of the form, it only takes a look at this lastChild and checks if the name-attributes matches the given value(createComment). If it does, the button will be removed.

后知后觉 2024-10-05 00:25:34

最终使用 jquery 代替:

function focusTextarea(id) {
    if($("#"+id+" #comment_"+id).val() == "Write a comment..") {
        $("#"+id+" #comment_"+id).val("");
        $("#"+id+" #commentTools_"+id).append("<input type='checkbox' name='createPrivate' value='1'> <span class='normal'>Private</span><br><input type='submit' name='createSubmit' value='Post comment' class='okButton'>");
        $("#"+id+" #comment_"+id).rows = 5;
    }
}

function unFocusTextarea(id) {
    if($("#"+id+" #comment_"+id).val() == "Write a comment.." || $("#"+id+" #comment_"+id).val() == "") {
        $("#"+id+" #commentTools_"+id).empty();
        $("#"+id+" #comment_"+id).val("Write a comment..");
    }
}

无论如何谢谢:)

Ended up using jquery instead:

function focusTextarea(id) {
    if($("#"+id+" #comment_"+id).val() == "Write a comment..") {
        $("#"+id+" #comment_"+id).val("");
        $("#"+id+" #commentTools_"+id).append("<input type='checkbox' name='createPrivate' value='1'> <span class='normal'>Private</span><br><input type='submit' name='createSubmit' value='Post comment' class='okButton'>");
        $("#"+id+" #comment_"+id).rows = 5;
    }
}

function unFocusTextarea(id) {
    if($("#"+id+" #comment_"+id).val() == "Write a comment.." || $("#"+id+" #comment_"+id).val() == "") {
        $("#"+id+" #commentTools_"+id).empty();
        $("#"+id+" #comment_"+id).val("Write a comment..");
    }
}

Thanks anyway :)

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