使用JavaScript来添加表单字段,但新名称是什么? [英] Using javascript to add form fields, but what will the new name be?

查看:167
本文介绍了使用JavaScript来添加表单字段,但新名称是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读这篇文章后:
使用javascript添加表单字段..但下面,而不是侧面?
我已经使按钮的工作!但我不知道如何接收输出。
这就是我输入的内容。

  var counter = 0; 
函数addNew(){
//获取所有其他div将被添加的主Div
var mainContainer = document.getElementById('mainContainer');
//创建一个用于保存文本和按钮输入元素的新div
var newDiv = document.createElement('div');
//创建一个新的文本输入
var newText = document.createElement('input');
newText.type =input;
newText.maxlength =50;
newText.maxlimit =50;
newText.size =60;
newText.value = counter;
//创建一个新的按钮输入
var newDelButton = document.createElement('input');
newDelButton.type =button;
newDelButton.value =删除;

//将新文本输入追加到newDiv
newDiv.appendChild(newText);
//将新的按钮输入添加到newDiv
newDiv.appendChild(newDelButton);
//将newDiv输入附加到mainContainer div
mainContainer.appendChild(newDiv);
counter ++;

//添加一个处理程序到按钮从mainContainer中删除newDiv
newDelButton.onclick = function(){
mainContainer.removeChild(newDiv);
}
}
< / script>

以下形式:

 < input type =buttonsize =3cols =30value =AddonClick =addNew()> 

那么,新的字段名称是什么?我对编码不够了解,不知道我在告诉它什么。



感谢任何回答。

解决方案

  newText.name =newInputName-+ counter; //对于名称
newText.id =newInputId-+ counter; //对于id

对于按钮

  newDelButton.name =btnDeleteName; //用于名称或btnDeleteName  - +多个
的计数器newDelButton.id =btnDeleteId; //为id或btnDeleteId - +多个
的计数器

名称 Id 对于任何物品都可以是相同的,即

  newText.name =newInput-+ counter; //对于名称
newText.id =newInput-+ counter; //为id


After reading this post: using javascript to add form fields.. but below, not to the side? I've made the button work! But I don't know how to receive the output. This is what I entered.

        var counter = 0;
        function addNew() {
            // Get the main Div in which all the other divs will be added
            var mainContainer = document.getElementById('mainContainer');
            // Create a new div for holding text and button input elements
            var newDiv = document.createElement('div');
            // Create a new text input
            var newText = document.createElement('input');
            newText.type = "input"; 
            newText.maxlength = "50"; 
            newText.maxlimit = "50"; 
            newText.size = "60"; 
            newText.value = counter;
            // Create a new button input
            var newDelButton = document.createElement('input');
            newDelButton.type = "button";
            newDelButton.value = "Delete";

            // Append new text input to the newDiv
            newDiv.appendChild(newText);
            // Append new button input to the newDiv
            newDiv.appendChild(newDelButton);
            // Append newDiv input to the mainContainer div
            mainContainer.appendChild(newDiv);
            counter++;

            // Add a handler to button for deleting the newDiv from the mainContainer
            newDelButton.onclick = function() {
                    mainContainer.removeChild(newDiv);
            }
        }
    </script>

With this in the form:

<input type="button"  size="3" cols="30" value="Add" onClick="addNew()">

So, what will the new field names be? I don't understand enough of the coding to figure out what I'm telling it to. Good thing there are other smart folks out there for me to lean on!

Thanks for any answers.

解决方案

newText.name = "newInputName-" + counter; // for name
newText.id = "newInputId-" + counter;   // for id

For button

newDelButton.name = "btnDeleteName"; // for name, or "btnDeleteName-"+counter for multiple
newDelButton.id = "btnDeleteId";     // for id    or "btnDeleteId-"+counter for multiple

Name and Id could be same for any item, i.e.

newText.name = "newInput-" + counter; // for name
newText.id = "newInput-" + counter;   // for id

这篇关于使用JavaScript来添加表单字段,但新名称是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆