使用javascript添加表单字段,但新名称是什么
Using javascript to add form fields, but what will the new name be?
看完这篇文章后:使用JavaScript添加表单字段。但是在下面,不是在一边?我让按钮工作了!但是我不知道如何接收输出。这是我输入的。
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>
以以下形式出现:
<input type="button" size="3" cols="30" value="Add" onClick="addNew()">
那么,新的字段名称将是什么?我对编码的理解还不够,无法弄清楚我在告诉它什么。还好还有其他聪明人可以依靠!
感谢您的任何回答。
newText.name = "newInputName-" + counter; // for name
newText.id = "newInputId-" + counter; // for id
"对于"按钮
newDelButton.name = "btnDeleteName"; // for name, or "btnDeleteName-"+counter for multiple
newDelButton.id = "btnDeleteId"; // for id or "btnDeleteId-"+counter for multiple
任何项目的Name
和Id
都可能相同,即
newText.name = "newInput-" + counter; // for name
newText.id = "newInput-" + counter; // for id
你可以
这样做:
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.id = "AddedInput_" + counter;
...
您必须为此元素添加"name"属性。你可以做类似的事情
newText.name = "newInput-" + counter;//这是另一个答案所说的。
这样,提交后,您将在服务器上收到newInput-0,newInput-1等
相关文章:
- 这个插件php wordpress中的全局类名是什么
- 是什么让新的Date() / 1000成为一个有效的javascript
- JavaScript函数中名为“undefined”的参数的目的是什么?
- 更改 URL 并使用 AJAX 获取响应加载新页面的最佳方法是什么
- 在 JavaScript 中创建具有重复列表条目的新列表的最快/推荐方法是什么
- 创建新模型的正确控制器/视图模式是什么
- 使用javascript添加表单字段,但新名称是什么
- ES5提供的新方法的实际用途是什么?
- 定义中的尖括号中的类名的用途是什么
- 在ie中使用类名查找tr的有效方法是什么
- 在使用AJAX获取html内容后,向新元素添加监听器的正确方法是什么?(jQuery,Javascript)
- 在AngularJS中创建新服务的优势是什么
- 在模块中创建新类的参数var名的正确方法是什么?
- 用jQuery制作新元素的正确方法是什么?
- 用jQuery追加新元素的安全方法是什么?
- 在jQuery中使用带有上下文节点的子选择器的新方法是什么?
- angular模块名中的点是什么意思?
- Jquery:选择新添加的DOM节点的最佳方法是什么?
- 如果从Parse Cloud调用外部api,原始主机名是什么?
- 使用jquery,获得“next”的最佳方法是什么?具有特定类名的文本框