首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

js 创建动态文本框,onclick怎么获取值

2012-12-16 
js 创建动态文本框,onclick如何获取值?如下代码,button的onclick能获取到button的值,想要:button onclick

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 = function() {testa(input);};就行了。也就是把this换成input
[其他解释]
获取什么文本框的值
[其他解释]
引用:
获取什么文本框的值

每一个button获取每一个text的值
[其他解释]
定义一个字符串、在点击按钮的时候、让Str = 文本框.setText
[其他解释]
试问,当你第一次点击的时候 input里面根本就没有值,你获取什么?
是不是应该添点什么 ,再获取值?   用onblur 可否?
[其他解释]
input2.onclick = function(){testa(this);};

换成
input2.onclick = "testa(this)";

试试
[其他解释]
引用:
试问,当你第一次点击的时候 input里面根本就没有值,你获取什么?
是不是应该添点什么 ,再获取值?   用onblur 可否?

input有值呀,看效果图

[其他解释]
没看清 题   不好意思 就当我没说,老毛病 又犯了 
[其他解释]

引用:

JavaScript code?1input2.onclick = function(){testa(this);};
换成
JavaScript code?1input2.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>



LZ,这样你点击生成的button,就可以获取对应前面input的值!
[其他解释]
引用:
JavaScript code?12345678910111213141516171819202122232425     var test=1     function CreateInput(){         var obj = document.getElementById("body_note");         nDiv = document.create……

这是一个办法,但我button的名称变了
[其他解释]
引用:
引用:JavaScript code?12345678910111213141516171819202122232425     var test=1     function CreateInput(){         var obj = document.getElementById("body_note");         n……

ID不是唯一,取值的话就会报错的
[其他解释]
引用:
JavaScript code?1234567891011121314151617181920212223242526272829303132333435363738394041424344<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</tit……

这样不对,只能获取到最后的值,你点add生成两个后再点button1看效果就知道了。
[其他解释]
引用:
引用:引用:JavaScript code?12345678910111213141516171819202122232425     var test=1     function CreateInput(){         var obj = document.getElementById("bo……

谢谢你的回复!

//原代码的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);
           
         }


这个试了?我测试都没问题,每个值改不改都能取到
[其他解释]
引用:
有一种简单的方法:
感觉 input2.onclick = function(){testa(this);};   这一句有问题。
把它换成input2.onclick = function() {testa(input);};就行了。也就是把this换成input

靠~真NB
[其他解释]
引用:
我就是要这种效果,谢谢17楼,,谢谢大家

+1威武了,呵呵!
[其他解释]
我就是要这种效果,谢谢17楼,,谢谢大家

热点排行
Bad Request.