简体   繁体   English

通过jquery查找控件ID,并将其传递给函数以对其进行切换

[英]find control id by jquery and pass it to function to toggle it

find control id by jquery and pass it to function to toggle it. 通过jquery查找控件ID,并将其传递给函数以进行切换。

 function toggleDiv() { var $usr = $this.find('[id*=shoow]'); $($usr).toggle('slow'); } 
 <asp:LinkButton runat="server" OnClientClick="toggleDiv(); return false;">Detials</asp:LinkButton> <table> <tr> <td id="shoow" style="width:650px;height:100px;background-color:blue; display:none;"> </td> </tr> </table> 

Initially, since you have declared a server side control, LinkButton , you have to provide an ID , for it. 最初,由于已声明服务器端控件LinkButton ,因此必须为其提供一个ID

<asp:LinkButton  ID="linkButtonId" runat="server">Details</asp:LinkButton>

You don't have to use the find function for this purpose. 您不必为此使用find函数。 Actually, we use this function for the following reason: 实际上,我们出于以下原因使用此功能:

Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element. 获取当前匹配元素集中每个元素的后代,并通过选择器,jQuery对象或元素进行过滤。

What you could do? 你能做什么?

I suggest at the bottom of your page before the body's closing tag to add this script 我建议在页面底部,在正文的结束标记之前添加此脚本

<script type="text/javascript">
    $(function(){
        $("#"+<%=linkButtonId.ClientID%>).click(function(){
            $('#shoow').toggle('slow');
        });
    })
</script>

Important Note 重要的提示

You should load the jQuery before this script. 您应该在此脚本之前加载jQuery。

i want to give id from code behind by find control function or jquery funtion becuase i have datalist generat id's random. 我想通过查找控制功能或jquery函数从代码后面给出ID,因为我有数据列表生成ID的随机性。

You could select then the element like below:

$("[id*="+"shoow"+"]").toggle('slow');

Try the following snippet to see what I mean: 尝试以下代码片段以了解我的意思:

 $(function(){ $("#buttonId").click(function(){ $("[id*="+"shoow"+"]").toggle('slow'); }) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="buttonId">Details</button> <table> <tr> <td id="3shoow2" style="width:650px;height:100px;background-color:blue; display:none;"> </td> </tr> </table> 

I agree the answer of Christos. 我同意克里斯托斯的回答。

Yes you don't have to use find function 是的,您不必使用find功能

Instead please use jQuery: 相反,请使用jQuery:

$('#shoow').toggle('slow');

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

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