2013-04-24 87 views
0
[pure javascript code without the html body] 
<script language="Javascript"> 
    [ creating buttons with the alphabets on it and its values the same] 

    for(var a=1;a<=26;a++) 
    { 
    document.write("<input type='button' size='1' value="+String.fromCharCode(a +64)+" id='btn"+a+"' onclick='see()' >"); 
    } 


document.write("<input type='text' size='10' id='box1' style='background-color:red'>")    

[normal textbox which should contain the data] 
    function see() 
     { 
     [trying to put the value of button on textbox on buttonclick but gives me undefined] 


     var text; 
     text=*document.getElementsByTagName*("button").value; 
     document.getElementById("box1").value=text; 
     } 

</script> 

我甚至憑身份證試圖document.getElementsByTagName或給出不確定的地方,因爲我想要的按鈕的值對文本框.. 我用for循環,因爲我必須創造約26按鈕按鈕值顯示未定義

+0

'getElementsByTagName'返回一個數組,所以'.value'不會像你所擁有的那樣工作。你需要循環遍歷文本數組,並執行text [index] .value – scrappedcola 2013-04-24 18:05:33

回答

1
"...value='" + String.fromCharCode(a +64) + "'..." 

您錯過了單引號。