2015-05-29 73 views
0

请不要jQuery。如何创建一个输入字段,然后预填充该字段

我已经看到了几个例子,其中输入字段已经存在于页面上,然后再更新它,但我想动态创建输入字段,然后用页面中提供的信息填充它(因此current_value上面这个代码段中定义变量):

for (i=0; i<tables.length; i++) { 
    (function(i) { 
    var sp = document.createElement('span'); 
    var act_table = document.createElement('table'); 
    act_table.id = 'act-tests-' + i; 
    act_table.style.display = 'none'; 

    var test_name_input = document.createElement('tr'); 
    test_name_input.innerHTML = '<tr><td width="100px">Name</td><td><input type="text" class="wideValue testName input"></td></tr>'; 
    test_name_input.setAttribute('value', current_rule); 

    act_table.innerHTML ='<tbody><tr>LOTS OF TABLE ROWS HERE</tr></tbody>'; 
    act_table.insertBefore(test_name_input,act_table.firstChild); 
    sp.appendChild(act_table); 
} 

该字段被示出,但不与我的current_value填充。

任何帮助非常感谢。

回答

3

在您的示例中,您将属性设置为tr,而不是创建的输入字段。 尝试这样做:

document.getElementsByClassName("input")[you_index].setAttribute("value", current_value); 

下面的例子:http://jsfiddle.net/35g0ks0t/

+0

谢谢你,我会在几个小时内尝试了这一点的时候,我可以取回它。 – Ramy