简体   繁体   English

如何使用JavaScript生成HTML表单?

[英]How do I use JavaScript to grow an HTML form?

I'm creating a simple web application with which a user may author a message with attached files. 我正在创建一个简单的Web应用程序,用户可以使用它来编写带有附件的消息。

multiple html file inputs with javascript link http://img38.imageshack.us/img38/4474/attachments.gif 带有javascript链接的多个HTML文件输入http://img38.imageshack.us/img38/4474/attachments.gif

That "attach additional files" link does not yet work. 该“附加其他文件”链接尚不起作用。 It should add an additional file input control to the form each time it's clicked. 每次单击时,都应向表单添加一个附加的文件输入控件。

I need a JavaScript method addanotherfileinput() to replace the clicked anchor: 我需要一个JavaScript方法addanotherfileinput()来替换单击的锚点:

        <a href="#" id="attachlink" onclick="addanotherfileinput">attach additional files</a>

With this new table row, file input, and anchor: 使用此新表行,文件输入和锚点:

        <input type="file" name="Attachment1" id="Attachment1" class="textinput" />
    </td>
</tr>
<tr>
    <td></td>
    <td>
        <a href="#" id="attachlink" onclick="addanotherfileinput">attach additional files</a>

And also increment the number: Attachment2, Attachment3, Attachment4, etc. 并增加数字:Attachment2,Attachment3,Attachment4等。

How can I do this? 我怎样才能做到这一点?

You could use the DOM to dynamically insert the file inputs 您可以使用DOM动态插入文件输入

var myTd = /* Get the td from the dom */

var myFile = document.createElement('INPUT');
myFile.type = 'file'
myFile.name = 'files' + i; // i is a var stored somewhere
i++;

myTd.appendChild(myFile);

OR you can use this 或者你可以使用这个

http://developer.yahoo.com/yui/examples/uploader/uploader-simple-button.html http://developer.yahoo.com/yui/examples/uploader/uploader-simple-button.html

There might be a better way, but I did this client side by creating a set of HTML form elements and then hiding/showing the rows using JavaScript to update the CSS class. 可能有更好的方法,但是我通过创建一组HTML表单元素,然后使用JavaScript隐藏/显示行来更新CSS类来完成此客户端操作。

EDIT: I'll leave this here as an alternate method but I like the idea of adding INPUT elements through the DOM better. 编辑:我将把它留在这里作为替代方法,但我更喜欢通过DOM添加INPUT元素的想法。

You probably don't want to replace the anchor, just insert something in front of it. 您可能不想替换锚,只需在锚前面插入一些东西。

I use Prototype for things like this, because it irons out a lot of browser inconsistencies, particularly around forms and tables. 我将Prototype用于此类事情,因为它消除了许多浏览器不一致的地方,尤其是围绕表单和表格的地方。

Using Prototype, it would look something like this: 使用原型,它看起来像这样:

function insertFileField(element, index) {
    element.insert({
        before: "<input type='file' name='Attachment" + index + " class='textinput'>"
    });
}

// And where you're hooking up your `a` element (`onclick` is very outdated,
// best to use unubtrustive JavaScript)
$('attachlink').observe('click', function(event) {
    event.stop();
    insertFileField(this, this.up('form').select('input[type=file]').length + 1);
});

...the bit at the end finds the form containing the link, finds out how many input s of type file there are, and adds one for the index. ...末尾的位找到包含链接的格式,找出有多少个类型file input ,并为索引添加一个。

There are other ways as well, via the Element constructor provided by Prototype, but text works quite well (in fact, it's usually much faster). 也可以通过Prototype提供的Element构造函数来进行其他操作,但是文本效果很好(实际上,它通常要快得多)。

It would be similar, though slightly different, with jQuery, MooTools, YUI, Glow, or any of the several other JavaScript frameworks. 它与jQuery,MooTools,YUI,Glow或其他几种JavaScript框架相似,但略有不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM