使用javascript函数参数填充Rails3中innerHTML标记的值字段

Using a javascript function argument to populate the value field of an innerHTML tag in Rails 3

本文关键字:字段 innerHTML 函数 javascript 参数 填充 Rails3 使用      更新时间:2023-09-26

我有一个jquery表,它允许我选择一条记录。当那个记录被选中时,我希望它的详细信息作为另一个记录出现在表中。我设法在控制器中初始化了一个变量:

 @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>

<%-end-%>

用户单击按钮后,将使用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}')" %>

2( 您的javascript中缺少了很多分号。考虑通过jslint运行javascript来捕获这些错误

3( 在单元格的css类中使用text-align:center,而不是<center>标记来居中单元格的内容

4( 如果您已经在使用jQuery,那么您可以使用大量的辅助函数。所以你应该调查一下。

但我不想重写你所有的代码,所以这里只是你需要的最低要求:

newCell.innerHTML = '<INPUT TYPE="TEXT" SIZE="10" NAME="Column1_'+intLine+'"  ID="Column1_'+intLine+'" VALUE="'+str+'">";

换句话说:设置文本字段的值属性(即内容(的方式与设置id和name属性的方式完全相同(我在字符串周围使用了单引号,以避免必须转义其中的所有双引号(

最后,除非您正在编写旧的HTML4,否则不需要使用所有大写的标记名和属性。<input type="text" ... >也同样有效,而且更容易阅读,我认为