我想向动态创建的textbox添加文本。

var dynamicTextBox= "";
    for (var i = 0; i < vm.FitToWork.length; i++) {
        dynamicTextBox+= '<input class="form-control"  name = "DynamicTextBox"  id=  "DynamicTextBox"  type="text" value = "'vm.FitToWork[i]'" />&nbsp;' +
        '<button id="btnAdd"   class="delete-decl">+</button>';

    }
    document.getElementById("TextBoxContainer").innerHTML=dynamicTextBox;


它不工作..

最佳答案

您有语法错误。您正在尝试连接变量vm.FitToWork[i],但未使用串联运算符(+)。尝试以下代码:



var dynamicTextBox= "";

// ignore this. just have this to get the code working
var vm = {FitToWork : ["test","rest","vest"]};

for (var i = 0; i < vm.FitToWork.length; i++) {
      dynamicTextBox += '<input class="form-control"  name = "DynamicTextBox"  id=  "DynamicTextBox"  type="text" value = "' + vm.FitToWork[i] + '" />';

      dynamicTextBox += '<button id="btnAdd"   class="delete-decl">+</button></br>';
}

document.getElementById("TextBoxContainer").innerHTML = dynamicTextBox;

<div id="TextBoxContainer"></div>

09-27 13:05