简体   繁体   English

使用Javascript访问ASP.NET控件

[英]accessing ASP.NET controls in Javascript

I have following Javascript line in my ASP.NET web app: 我的ASP.NET Web应用程序中包含以下Java语言行:

document.getElementById('<%=myTextBox[0].ClientID %>').value = "test";

how can I access myTextBox elements? 如何访问myTextBox元素? for instance I want to change 5th element of this server side array, I want to pass a server side parameter to my function, how can I do it? 例如,我想更改此服务器端数组的第5个元素,我想将服务器端参数传递给我的函数,该怎么办?

for instance: 例如:

server side:
ddl.Attributes.Add("onChange", "return OnFoodChange(this,'" + i + "');");


javascript function:
function OnFoodChange(myCmb,myIndex)

using document.getElementById('<%=myTextBox[myIndex].ClientID %>').value = "test"; 使用document.getElementById('<%=myTextBox[myIndex].ClientID %>').value = "test"; gives me error, it says that myIndex is not defined, I think because myIndex is used like a server side parameter, how can I solve it? 给我错误,它表示未定义myIndex,我认为因为myIndex的用法类似于服务器端参数,我该如何解决呢?

it is my full JavaScript function: 这是我完整的JavaScript函数:

   function OnFoodChange(myCmb,myIndex) {
               //alert('5');
try{
               var q = document.getElementById('<%= HFFoodPrice.ClientID %>').value.toString();
           var q2 = q.split(';');
           var index = 0;
           //alert(myCmb.selectedIndex.toString());

           //var e = document.getElementById(myCmb);
           var strUser = myCmb.options[myCmb.selectedIndex].value;

           document.getElementById('<%=myTextBox[0].ClientID %>').value = strUser;

           for (var j = 0; j < q2.length; j++) {
               if (q2[j] != '') {
                   var q3 = q2[j].split(',');
                   {

                   }
               }
           }
           }
           catch(err)
           {
           alert(err.message);
           }
       }

Send this control id of textbox instead of sending index as asp.net control id might not be as you expect, 发送此文本框控件ID而不是发送索引,因为asp.net控件ID可能不符合您的预期,

In Code behind 在后面的代码中

ddl.Attributes.Add("onChange", "return OnFoodChange(this,'" + myTextBox[i].ClientID + "');");

In HTML 在HTML中

function OnFoodChange(myCmb,myTextBoxId) {
 currentTextBox = document.getElementById(myTextBoxId);
//Your code...
}

You are trying to Mix Server side with Client side. 您正在尝试将服务器端与客户端混在一起。 it does not work like this. 它不能像这样工作。 you need to transfer your server array to javascript on server then you will be able access its index on clien side. 您需要将服务器数组传输到服务器上的javascript,然后才能在客户端上访问其索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM