开发者

calling java script function from ajax method

in code behind :

  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'开发者_Go百科   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>");

java script function in aspx page :

 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 )'

i could able to pass the value of textboxrow_icol_2 ,

i need to pass the values of other 2 boxes in the same function textboxrow_icol_1 & textboxrow_icol_3

how to do it.. please help me out to solve this isue


If I understand you correctly, you only need the onchange on one of your textboxes, but within its handler you want to be able to access the values from three textboxes in the same row?

In your server side code do this:

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

So that you just pass the row number to your change_text() function. Then in your client-side code do this:

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
}

Alternatively you can do onchange='return change_text(this);', passing the function a reference to the input directly instead of its id or row number, and then define your function something like:

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.
}

EDIT: If you copy and paste my first example it won't work because I got the capitalisation wrong and left out an underscore for your input id attributes. So where I had "Col1" it should be "col_1" (and so forth for col_2, col_3).


Add an id to the row:

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

Then add an attribute to the input elements with it's correcponding row id and a class so you can tell the 3 apart:

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

Then pass the row id to the function handeling the on text change:

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

Then add the function to the event handlers for the input fields:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜