使用C#codeBehind有返回值调用jQuery函数 [英] Calling jQuery function using C# CodeBehind with return value

查看:109
本文介绍了使用C#codeBehind有返回值调用jQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有将用于显示来自关于各个网站的水公司的服务器的信息的ASP.NET应用程序。我有一个jQuery的方法,它返回已被点击的div信息中的超链接的文本:

I have an ASP.NET application that will be used to display information from a server regarding various sites for a water company. I have a jQuery method that returns the text of the hyperlink which has been clicked within the div 'info':

<script type="text/javascript">
        $('#info a').click(function getName()
        {
            return ($(this).text());
        });
</script>

我可以用code。使用C#codebehind调用此方法

I can call this method using C# codebehind using the code

ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "getName()", true);

不过,我不能得到它的返回值,这正是我需要的。任何人都可以阐明这一些轻?

However I cannot get its return value, which is what I need. Can anyone shed some light on this?

推荐答案

使用隐藏域:

<input type="hidden" id="myhiddenField" name="myhiddenField" runat="server" />

和jQuery(的没有测试的):

<script type="text/javascript">
        $('#info a').click(function getName()
        {
            $("#myhiddenField").val($(this).text());
        });
</script>

和那么你就可以访问隐藏字段在code后面 myhiddenField.Value

And then you would be able to access hidden field in code behind myhiddenField.Value.

或者,如果你想使用Ajax调用看到教程的这里

Or if you want to use Ajax Call see tutorial here

修改

EDIT :

我创建了一个小项目和下面的工作正常,我(我得到警报测试):

I created a little project and the below works fine for me (I get alert "testing"):

 <script type="text/javascript">
        $(document).ready(function () {
            $('#info a').click(function getName() {
                // As control having runat="server" their ids get changed
                // selector would be like this 
                $("#<%= myhiddenField.ClientID %>").val($(this).text());
                alert($("#<%= myhiddenField.ClientID %>").val());
            });
        });
</script>

<div id="info">
  <a href="#">testing</a>
</div>
<input type="hidden" id="myhiddenField" name="myhiddenField" runat="server" />

这篇关于使用C#codeBehind有返回值调用jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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