简体   繁体   English

Javascript asp.net C#多对象传递到Js代码文件

[英]Javascript asp.net c# multi objet pass to Js code file

I have code on html page and it is working: 我在html页面上有代码,并且可以正常工作:

document.getElementById('<%=IdOstan.ClientID %>').options.length = 0;

it is part of a function. 它是功能的一部分。 Object is found and code run. 找到对象并运行代码。 But if I move code to a seperate js file, then call the file using : 但是,如果我将代码移到单独的js文件中,请使用调用该文件:

<script src="../../../../Script/AjaxCall.js" type="text/javascript"></script>

then object is not found. 则找不到对象。 My conclusion was that Object on the same file can be found with calling document.getElementById , but it is not true about object passed to Js, and seems normal. 我的结论是,可以通过调用document.getElementById来找到同一文件上的对象,但是对于传递给Js的对象来说并非如此,这似乎很正常。 I have tried to pass object while calling JS function like this : 我试图在调用JS函数时传递对象,如下所示:

<asp:DropDownList ID="IdLand" runat="server" onchange="UpdateOstan(this);" 
     DataTextField="NameLand" DataValueField="IdLand" ViewStateMode="Enabled" 
     AutoPostBack="false">
</asp:DropDownList>

and it also works. 而且也可以。 But I need to pass a second object too I have tried : 但是我也尝试过传递第二个对象:

onchange="UpdateOstan(this, IdsecondObj);"

which failed, also this 失败了,也是这个

onchange="UpdateOstan(this, document.getElementById('IdSecondObject'));"

not working. 不工作。 So pleas let me know how to pass html object to Javascript which is not on the same page. 所以请让我知道如何将html对象传递给不在同一页面上的Javascript。 I don't want to use jQuery files as helper,at all. 我根本不想使用jQuery文件作为帮助程序。 Thanks 谢谢

Use hidden input to store ID value of your IdOStan.ClientID and get that value for id name from your external javascript and then you can access that object. 使用隐藏的输入来存储IdOStan.ClientID的ID值,并从外部javascript获取该ID名称的值,然后就可以访问该对象。

<input id="MyHidden" value="<%=IdOstan.ClientID %>" />

In external JS file. 在外部JS文件中。

var id = document.getElementById('MyHidden').value;
var IdOstan = document.getElementById(id);

Now IdOstan is the object you are looking for. 现在,IdOstan是您要查找的对象。

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

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