js 创建动态文本框,onclick如何获取值?
如下代码,button的onclick能获取到button的值,
想要:button onclick的时候能获取文本框的值,该怎么写?
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
//创建 input 文本框
var test=1
function CreateInput(){
var obj = document.getElementById("body_note");
nDiv = document.createElement("div");
nDiv.id="showText"+test;
obj.appendChild(nDiv);
var input = document.createElement("input");
input.type = "text";
input.id = "inputText";
input.value = test;
var input2 = document.createElement("input");
input2.type = "button";
input2.id = "button"+test;
input3=input2.id
input2.value = "button"+test;
input2.onclick = function(){testa(this);};
document.getElementById("showText"+test).appendChild(input);
document.getElementById("showText"+test).appendChild(input2);
test++;
}
function testa(t){
alert (t.value)
}
</script>
</head>
<body id="body_note" onload="CreateInput()">
<input type="button" name="test" id="test" value="add" onclick="CreateInput()" />
</body>
input2.onclick = function(){testa(this);};input2.onclick = "testa(this)";
var test=1
function CreateInput(){
var obj = document.getElementById("body_note");
nDiv = document.createElement("div");
nDiv.id="showText"+test;
obj.appendChild(nDiv);
var input = document.createElement("input");
input.type = "text";
input.id = "inputText"+test;
input.value = test;
var input2 = document.createElement("input");
input2.type = "button";
input2.id = "button"+test;
input3=input2.id
input2.value = test;
input2.onclick = function(){testa(this);};
document.getElementById("showText"+test).appendChild(input);
document.getElementById("showText"+test).appendChild(input2);
test++;
}
function testa(t){
alert(document.getElementById("inputText"+t.value).value);
}
input.id = "inputText"+test;
function testa(t){
alert(document.getElementById("inputText"+t.value).value);
}
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
//创建 input 文本框
var test=1
function CreateInput(){
var obj = document.getElementById("body_note");
nDiv = document.createElement("div");
nDiv.id="showText"+test;
obj.appendChild(nDiv);
var input = document.createElement("input");
input.type = "text";
input.id = "inputText"+test;
input.value = test;
var input2 = document.createElement("input");
input2.type = "button";
input2.id = "button"+test;
input3=input2.id
input2.value = "button"+test;
input2.onclick = function(){testa(test-1);};
document.getElementById("showText"+test).appendChild(input);
document.getElementById("showText"+test).appendChild(input2);
test++;
}
function testa(t){
var value=document.getElementById("inputText"+t).value;
alert (value)
}
</script>
</head>
<body id="body_note" onload="CreateInput()">
<input type="button" name="test" id="test" value="add" onclick="CreateInput()" />
</body>
//原代码的input id改为动态的
input.id = "inputText"+test;
var test=1
function CreateInput(){
var obj = document.getElementById("body_note");
nDiv = document.createElement("div");
nDiv.id="showText"+test;
obj.appendChild(nDiv);
var input = document.createElement("input");
input.type = "text";
input.id = "inputText"+test;
input.value = test;
var input2 = document.createElement("input");
input2.type = "button";
input2.id = "button"+test;
input3=input2.id
input2.value = test;
input2.onclick = function(){testa(this);};
document.getElementById("showText"+test).appendChild(input);
document.getElementById("showText"+test).appendChild(input2);
test++;
}
function testa(t){
alert(document.getElementById("inputText"+t.value).value);
}