2014-07-05 122 views
0

我有这样的代码 (脚本):添加文本框

var response = [{ 
     "A":"a2", 
     "B":"b2", 
     "C":"c2" 
    }, 
    { 
     "A":"a3", 
     "B":"b3", 
     "C":"c3" 
    }, 
    { 
     "A":"a4", 
     "B":"b4", 
     "C":"c4" 
    }]; 

    $.each(response, function(i, item) { 
       $('<tr>').html(
       //"<tr>" + 
       "<td>" + response[i].A + "</td><td>" + response[i].B + "</td><td>" + response[i].C + "</td>" + "</tr>").appendTo('#MyTable tbody'); 
     }); 

,我想更改列的一个(只是为了记录它的将是“A”)是通过文本框“编辑”。 我这样做:

...    `"<td><input type="text">" + response[i].A + "</td><td>" + response[i].B + "</td><td>" + response[i].C + "</td>" + "</tr>").appendTo('#MyTable tbody');`... 

但它不工作。 请任何帮助!

+0

您需要分配一个值到输入: bitsm

+0

观看引号。由于您使用'“...”来分隔Javascript字符串,因此您必须使用“...”或“\”... \“'来分隔属性值。 – Barmar

+0

你是否试图创建一个新的''与内容..? –

回答

0

如果我理解正确,

而不是创造<tr>,以后使用html()添加内容,并最终追加到做<table>的,可以直接使用完全的方法append()都添加到<table>

$.each(response, function(i, item) { 
    $('#MyTable tbody').append("<tr>" 
      +"<td><input type='text' value='"+ response[i].A +"'/> </td><td><input type='text' value='"+ response[i].B +"'/></td><td><input type='text' value='"+ response[i].C +"'/></td>" 
      + "</tr>"); 
    }); 
0

试着说这

var firstcell = "<td><input type="text" value="VALUE"</td>"; 
firstcell = firstcell.replace('VALUE', response[i].A); 

使用此在每行第一个单元格。之后,将其附加在整行中。