使用javascript函数参数填充Rails 3中innerHTML标记的值字段
我有一个jquery表,可以让我选择一条记录。选中该记录后,我希望其详细信息作为另一条记录显示在表中。我设法在控制器中初始化了一个变量:使用javascript函数参数填充Rails 3中innerHTML标记的值字段,javascript,ruby-on-rails-3,Javascript,Ruby On Rails 3,我有一个jquery表,可以让我选择一条记录。选中该记录后,我希望其详细信息作为另一条记录显示在表中。我设法在控制器中初始化了一个变量: @var ||= [] 然后使用项目对象中的数据填充该变量: <% @projects.each do |project| %> <tr> <td><%= project.project_number %></td> <td><%= pro
@var ||= []
然后使用项目对象中的数据填充该变量:
<% @projects.each do |project| %>
<tr>
<td><%= project.project_number %></td>
<td><%= project.project_name %></td>
<% @var = project.project_name %>
<td><%= link_to_function image_tag("icons/add.png"), "CreateNewRow('var')" %></td>
<!-- link_to image_tag("icons/add.png"), tasklist_path(project.id), :as => "tasklist" -->
</tr>
用户单击按钮后,将立即使用CreateNewRow函数:
function CreateNewRow(str)
{
var intLine = parseInt(document.frmMain.hdnMaxLine.value);
intLine++;
var theTable = document.getElementById("tbExp");
var newRow = theTable.insertRow(theTable.rows.length)
newRow.id = newRow.uniqueID
var newCell
var argument = str
//*** Column 1 ***//
newCell = newRow.insertCell(0);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"10\" NAME=\"Column1_"+intLine+"\" ID=\"Column1_"+intLine+"\" VALUE=\"\"></center>";
//*** Column 2 ***//
newCell = newRow.insertCell(1);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"Column2_"+intLine+"\" ID=\"Column2_"+intLine+"\" VALUE=\"\"></center>";
newCell.innerHTML = "<center><SELECT NAME=\"Column5_"+intLine+"\" ID=\"Column5_"+intLine+"\"></SELECT></center>";
//*** Column 3 ***//
newCell = newRow.insertCell(2);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"Column3_"+intLine+"\" ID=\"Column3_"+intLine+"\" VALUE=\"\"></center>";
ID=\"Column5_"+intLine+"\"></SELECT></center>";
//*** Create Option ***//
CreateSelectOption("Column5_"+intLine)
document.frmMain.hdnMaxLine.value = intLine;
}
我的问题是如何从javascript函数中获取参数以在innerHTML值字段中显示为文本。。或者最好是某种标签?获取参数并将其显示在表格单元格中的某些内容。。。我不知所措……1您的rails视图没有在javascript中设置var变量。它只是在rails中设置了一个var变量。甚至是一个实例变量。这些都没有意义。就这么做吧
<%= link_to_function image_tag("icons/add.png"), "CreateNewRow('#{project.project_name}')" %>
换言之:设置value属性,即文本字段的内容,方法与设置id和name属性时完全相同,我在字符串周围使用了单引号,以避免转义其中的所有双引号
最后,除非您正在编写旧的HTML4,否则不需要使用所有大写标记名和属性。同样有效,而且更容易阅读,我想谢谢你,最后我做了newCell.innerHTML=argument,,似乎也一样有效well@spartan2417:好的,我以为您希望项目名称显示在新单元格的文本字段中,而不仅仅是新单元格中的文本。在任何情况下,var参数=str行都是毫无意义的。您也可以编写newCell.innerHTML=str;。不管怎样,我已经更新了我的答案,我注意到了更多的几点。。。是的,我理解了参数=str,是突出来的乱七八糟,忘了把它恢复到正常的str。但是你说的第一点是我遗漏了什么。非常感谢!!!!
newCell.innerHTML = '<INPUT TYPE="TEXT" SIZE="10" NAME="Column1_'+intLine+'" ID="Column1_'+intLine+'" VALUE="'+str+'">";