我想为动态创建的文本框添加标签。怎么做?
的JavaScript
$function add() {
//Create an input type dynamically.
$var element = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute("type", "text");
element.setAttribute("name", "speaker[]");
var foo = document.getElementById("fooBar");
//Append the element in page (in span).
foo.appendChild(element);
}
答案 0 :(得分:0)
在范围中添加一个文本节点:
<html>
<head>
<script type='text/javascript'>
function addThings()
{
var label = document.createElement("div");
label.innerHTML = "label"; // you can set class name or style to align the DIV
var element = document.createElement("input");
element.setAttribute("type", "text");
element.setAttribute("name", "speaker[]");
var foo = document.getElementById("fooBar");
//Append the element in page (in span).
foo.appendChild(label);
foo.appendChild(element);
}
</script>
</head>
<body>
<div id="fooBar"></div>
<br/>
<a href="#" id="myButton" onclick='addThings()'>Add Label and Text</a>
</body>
</html>
答案 1 :(得分:0)
你只需要使用jquery
追加这样的元素$(function(){
$('.clickclass').click(function(){
$('.classofspan').append("<lable>lable of text filed</lable>\
<input type='text' name='anything[]' >");
});
});
然后,如果您想在点击事件上添加此添加,您可以执行此操作
<a href="javascript:;" class="clickclass">Add Field</a>
您还可以通过向您的操作添加课程来执行操作
<span class="classofspan"></span>
只要按下Add Filed
链接,它就会在span中添加输入字段