如何在多个控件中使用相同的javascript函数 [英] How to use same javascript function in multiple controls

查看:111
本文介绍了如何在多个控件中使用相同的javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过此示例代码,但这是不正确的...

I have tried this sample code but this is not correct...

<div>
        <asp:TextBox ID="TextBox1" runat="server" />
        <asp:Button ID="Button1" OnClientClick="getControlID('<%=TextBox1.ID %>');" runat="server" Text="Click Here" />
        <asp:TextBox ID="TextBox2" runat="server" />
        <asp:Button ID="Button2" OnClientClick="getControlID('<%=TextBox2.ID %>');" runat="server" Text="Click Here" />
</div>


<script type="text/javascript">
        function getControlID(obj) {
            var txtControl = document.getElementById(obj);
            txtControl.value = "It's working!";
            return false;
        }
    </script>


当我单击Button1时,我想显示它正在工作!"消息在TextBox1中或当我单击Button2时,我想使用单个功能在TextBox2中显示该消息.


When I click Button1 then I want to display "It''s working!" message in TextBox1 or when I click Button2 then I want to display that message in TextBox2 with using single function.

推荐答案

您的代码几乎在那里.进行以下细微改动:

Your code is almost there. Make the following slight alteration:

<%=TextBox1.ClientID %>

<%=TextBox2.ClientID %>



代替:



instead of:

<%=TextBox1.ID %>

<%=TextBox2.ID %>



有关ClientID的更多信息:

http://msdn.microsoft. com/en-us/library/system.web.ui.control.clientid%28v = vs.100%29.aspx [



More info about ClientID:

http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid%28v=vs.100%29.aspx[^]


问题是TextBox2.ID返回控件的服务器端ID ID,而不是返回给浏览器的ID.如果您在Chrome中打开页面,请右键单击该按钮,然后选择检查元素"或查看源代码,您将明白我的意思.



您可以这样做:

The problem is TextBox2.ID returns the sever-side ID of the control, not the one rendered to the browser. If you open you page in Chrome , right click the button and select "Inspect Element" or view the source code, you''ll see what I mean.



You could do this:

<asp:Button ID="Button1" OnClientClick="getControlID('<%=TextBox1.ClientID %>');" runat="server" Text="Click Here" />
<asp:TextBox ID="TextBox2" runat="server" />
<asp:Button ID="Button2" OnClientClick="getControlID('<%=TextBox2.ClientID %>');" runat="server" Text="Click Here" />



获取客户端ID.

就我个人而言,我认为以下代码更容易忽略代码中的任何错误:



To get the client ID.

Personally, I think the following is simpler ignoring any errors I have in the code:

<div>
        <asp:TextBox ID="TextBox1" runat="server" />
        <asp:Button ID="Button1" OnClientClick="getControlID(this);" runat="server" Text="Click Here" />
        <asp:TextBox ID="TextBox2" runat="server" />
        <asp:Button ID="Button2" OnClientClick="getControlID(this);" runat="server" Text="Click Here" />
</div>
<script type="text/javascript">
        function getControlID(control) {
            control.value = "It's working!";
            return false;
        }
    </script>


假设我做对了(我还没有测试代码),按钮本身将被传递,从而不需要GetById.

进一步讲,您可以了解一下jquery:它确实简化了客户端工作.


Assuming I''ve got it right (I haven''t tested the code) the button itself will be passed, removing the need for the GetById.

On o further point, you sould take a look at jquery: it really simplifies client work.


这篇关于如何在多个控件中使用相同的javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆