从ajax方法调用javascript函数

时间:2011-05-10 05:23:18

标签: asp.net-ajax

代码背后的

  b.Append("<table style='background-color:#f3f3f3; border: #336699 3px solid; ");
            b.Append("width:80%; font-size:10pt; font-family:Verdana;' cellspacing='0' cellpadding='3'>");
            b.Append("<tr><td colspan='9' align ='center'  style='background-color:#336699; color:white;'>");
            b.Append("<b>Machine Counter</b>");
            b.Append("</td></tr>");
            b.Append("<td><b>MACHINENAME</b></td>");
            b.Append("<td><b>START</b></td>");
            b.Append("<td><b>END</b></td>");
            b.Append("<td><b>TOTAL_RUNTIME</b></td></tr>");

            for (int i = 0; i < newds.Tables[0].Rows.Count; i++)
            {

                b.Append("<tr>");
                b.Append("<td>" + newds.Tables[0].Rows[i]["machinename"].ToString() + "</td>");
                if (newds.Tables[0].Rows[i][3].ToString() != "Y")
                {
                    b.Append("<td><input type = 'textbox' READONLY = 'readOnly'   id = 'TextBoxRow_" + i + "col_" + 1 + " '   value = '" + newds.Tables[0].Rows[i]["end_counter"].ToString() + "'   /></td>");
                }
                else
                {
                    b.Append("<td><input type = 'textbox' READONLY = 'readOnly'    id = 'TextBoxRow_" + i + "col_" + 1 + " '   value = '" + 0 + "'   /></td>");                 
                }

                b.Append("<td><input type = 'textbox'   id = 'TextBoxRow_" + i + "col_" + 2 + " '   value = '" + 0 + "'  onkeypress  = 'return checkNum(this.id)'   onchange = 'return change_text( ("+i+")'  /></td>"); //   onchange = ' return change_text(" + i + " )'


                if (newds.Tables[0].Rows[i][3].ToString() != "Y")
                {
                    b.Append("<td ><input type = 'textbox' READONLY = 'readOnly'   id = 'TextBoxRow_" + i + "col_" + 3 + " '   value = '" + newds.Tables[0].Rows[i]["end_counter"].ToString() + "'   /></td>");
                }
                else
                {
                    b.Append("<td ><input type = 'textbox' READONLY = 'readOnly'  id = 'TextBoxRow_" + i + "col_" + 3 + " '   value = '" + 0 + "'   /></td>");
                }
                b.Append("</tr>");
            }
            b.Append("</table>");

aspx页面中的java脚本函数:

 function change_text(i)
  {
      var txtvalue1 = document.getElementById("TextBoxRow_"+i+"col_1");    
      var txtvalue2 = document.getElementById("TextBoxRow_"+i+"col_2");   
      var txtvalue3 = document.getElementById("TextBoxRow_"+i+"col_3").value;          
   }

problm:

in function onchange ='return change_text(this.id)'

我能够传递textboxrow_icol_2的值,

我需要在同一个函数textboxrow_icol_1&amp;中传递其他2个框的值。  textboxrow_icol_3

怎么做..请帮我解决这个问题

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您只需要在其中一个文本框上使用onchange,但在其处理程序中,您希望能够访问同一行中三个文本框中的值吗?

在服务器端代码中执行以下操作:

b.Append("<td><input type = 'textbox' id = 'TextBoxRow_" + i + "col_" + 2 + " ' value = '" + 0 + "' onkeypress = 'return checkNum(this.id)'  onchange = 'return change_text(" + i + ")'  /></td>"); 

这样您只需将行号传递给change_text()函数即可。然后在客户端代码中执行以下操作:

function change_text(i) {
   var txtvalue1 = document.getElementById("TextBoxRow_" + i + "Col1").value;
   var txtvalue2 = document.getElementById("TextBoxRow_" + i + "Col2").value;
   var txtvalue3 = document.getElementById("TextBoxRow_" + i + "Col3").value;

   // use values as appropriate, parse as floats, whatever
}

或者你可以做onchange='return change_text(this);',直接传递函数对输入的引用而不是它的id或行号,然后定义你的函数:

function change_text(myInput) {
   var parentTD = myInput.parent;
   var parentTR = parentTD.parent; // or myInput.parent.parent;
   var txtvalue1 = parentTR.cells[1].firstChild.value;
   var txtvalue2 = myInput.value;
   var txtvalue3 = parentTR.cells[3].firstChild.value;
   // Note: the above is just one way to work your way through the DOM
   // to get to the elements you care about, and I did that off the top
   // of my head without testing it so I may have got it wrong.
   // You should Google getElementsByTagName(), childNodes, firstChild, etc.
   // to learn about how to access DOM structure through your code.
}

编辑:如果你复制并粘贴我的第一个例子,它将无法正常工作,因为我的大小写错误,并为你的输入id属性省略了下划线。所以,我"Col1"的位置应为"col_1"(对于col_2,col_3等等)。

答案 1 :(得分:0)

在行中添加ID:

b.Append("<tr id="row" + i +"><td colspan='9' align ='center'  style='background-color:#336699; color:white;'>");

然后使用相应的行id和类为输入元素添加一个属性,这样你可以分辨出3:

b.Append("<td><input type = 'textbox'   id = 'TextBoxRow_" + i + " rowid=row" + i + " class="textbox1"......

然后将行id传递给handeling on text change的函数:

function change_text(rowid){
   var textbox1_val = $('#' + rowid + ' input.textbox1').val(); 

然后将该函数添加到输入字段的事件处理程序中:

$('.textbox1.textbox2').change(function change_text(this.rowid){....});