问题描述
我有以下的Javascript线在我的ASP.NET web应用程序:
的document.getElementById('<%= myTextBox [0] .ClientID%GT;')。值=测试;
我怎么能访问myTextBox元素呢?比如我想改变这种服务器端阵列的第5个元素,我想一个服务器端的参数传递给我的功能,我该怎么办呢?
例如:
服务器端:
ddl.Attributes.Add(的onChange,返回OnFoodChange(这一点,'+ I +'););
javascript函数:
功能OnFoodChange(myCmb,myIndex)
。值=测试; 给我的错误,它说,使用 这myIndex没有定义,我想是因为myIndex的使用方式与服务器端的参数,我该怎么解决呢? 这是我的全JavaScript函数:的document.getElementById(';%= myTextBox [myIndex] .ClientID%GT&LT')
函数OnFoodChange(myCmb,myIndex){
//警报(5);
尝试{
变种Q =的document.getElementById('&下;%= HFFoodPrice.ClientID%GT;')。value.toString();
无功Q2 = q.split(';');
变种索引= 0;
//警报(myCmb.selectedIndex.toString()); //变种E =的document.getElementById(myCmb);
VAR strUser的= myCmb.options [myCmb.selectedIndex] .value的; 的document.getElementById('<%= myTextBox [0] .ClientID%GT;')。值= strUser的; 对于(VAR J = 0; J< q2.length; J ++){
如果(Q2 [J]!=''){
VAR Q3 = Q2 [J] .split('');
{ }
}
}
}
赶上(错误)
{
警报(err.message);
}
}
发送,而不是发送指数asp.net控件ID像您期望可能不是文本框的这个控件的ID,
在code后面
ddl.Attributes.Add(的onChange,返回OnFoodChange(这一点,'+ myTextBox [I] .ClientID +'););
在HTML
函数OnFoodChange(myCmb,myTextBoxId){
currentTextBox =的document.getElementById(myTextBoxId);
//你的code ...
}
I have following Javascript line in my ASP.NET web app:
document.getElementById('<%=myTextBox[0].ClientID %>').value = "test";
how can I access myTextBox elements? 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?
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";
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?
it is my full JavaScript function:
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,
In Code behind
ddl.Attributes.Add("onChange", "return OnFoodChange(this,'" + myTextBox[i].ClientID + "');");
In HTML
function OnFoodChange(myCmb,myTextBoxId) {
currentTextBox = document.getElementById(myTextBoxId);
//Your code...
}
这篇关于访问在Javascript中的ASP.NET控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!