0

嗨,我有一个网络表单,我需要根据前一个文本框中的条目动态添加多个文本框。即使在前一个文本框中输入了一个字符,它也应该在它旁边生成新的文本框,而不会失去前一个文本框的焦点,它应该允许我无限制地输入尽可能多的字符。这是我的代码:

    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 个字符并将焦点转移到新的文本框上。每次我尝试在文本框中输入多个字符时,它都会为每个字符创建新的文本框。有什么解决办法??

4

1 回答 1

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. 让每个 TextBox 中的 TextBox 只添加一次

于 2013-05-09T14:25:42.203 回答