2013-05-09 37 views
0

您好我有一個web窗體,我需要根據上一個文本框中的條目動態添加多個文本框。即使輸入了一個字符以前的文本框應該產生它旁邊新texbox沒有從以前的文本框失去焦點,它應該讓我不限制進入儘可能多的字符.. 這裏是我的代碼:動態創建多個文本框,取決於以前的文本框中的最小字符數/ javascript/asp

getId = function() 
    { 
     var id = 1; 
     return function() 
     { 
      id++; 
     } 
    } 

    function CreateTextbox() 
    { 
     var box = document.getElementById("divCreateTextbox"); 
     var curr = 'txt' + getId(); 
     var inp = document.createElement('input'); 

     inp.type = 'text'; 
     inp.name = 'textfield'; 
     inp.setAttribute("id", curr); 
     inp.setAttribute("minlength",'1'); 
     box.appendChild(inp); 
     inp.setAttribute('onkeyup', 'moveOnMin(this)'); 
     inp.focus(); 

    } 

    function moveOnMin(s) 
    { 
     if(s.value.length >= parseInt(s.getAttribute("minlength"))) 
     { 

     CreateTextbox(); 
     } 

問題與上面的代碼是,它只是讓我輸入1個字符一個文本框,並將焦點轉移到新的文本框上。每次我嘗試在文本框中輸入多個字符時,都會爲每個字符創建新的文本框。任何解決方案

+0

我打電話的功能

只加一次<腳本類型= 「文本/ JavaScript的」> 的window.onload =函數() { CreateTextbox() } – Rithu 2013-05-09 13:14:53

回答

0

試試這個。

function CreateTextbox() 
{ 
    var box = document.getElementById("divCreateTextbox"); 
    var curr = 'txt' + getId(); 
    var inp = document.createElement('input'); 

    inp.type = 'text'; 
    inp.name = 'textfield'; 
    inp.setAttribute("id", curr); 
    inp.setAttribute("minlength",'1'); 
    box.appendChild(inp); 
    inp.setAttribute('onkeyup', 'moveOnMin(this)'); 
    inp.setAttribute("textBoxAdded", "0"); 
    //inp.focus(); 

} 

function moveOnMin(s) 
{ 
    if (s.value.length == parseInt(s.getAttribute("minlength")) && s.getAttribute("textBoxAdded") == "0") 
{ 
     CreateTextbox(); 
     s.setAttribute("textBoxAdded", "1"); 
     s.focus(); 
    } 
} 

我已將您的代碼更改爲: 1.繼續關注現有的TextBox;和 2.讓文本框從每一個文本框

+0

感謝阿里,但其不添加任何atall文本框.. – Rithu 2013-05-09 14:34:23

+0

我已經在IE 8.0測試它。您正在測試哪個瀏覽器? – 2013-05-09 14:51:10

+0

正如你在上面所說的「......每次我嘗試在文本框中輸入多個字符時,它都會爲每個字符創建新的文本框。」,表示代碼早就在你身邊工作了。我剛剛做了一些改變。 – 2013-05-09 14:52:59