Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用JavaScript查找具有多个ASP UserControl的ID_C#_Javascript_Asp.net - Fatal编程技术网

C# 使用JavaScript查找具有多个ASP UserControl的ID

C# 使用JavaScript查找具有多个ASP UserControl的ID,c#,javascript,asp.net,C#,Javascript,Asp.net,我有一个ASP用户控件名ListItem。每个ListItem上都有两个ASP控件:一个标签lblIndex和一个按钮btnAction 在我的页面上,我将10个ListItem加载到一个面板中,并在每个列表面板上设置一个适当的索引 ListItem 1 : lblIndex.value = '#1' ListItem 2 : lblIndex.value = '#2' ... ListItem 10 : lblIndex.value = '#10' 如何编写Javascript,每次单击列表

我有一个ASP用户控件名
ListItem
。每个
ListItem
上都有两个ASP控件:一个标签
lblIndex
和一个按钮
btnAction

在我的页面上,我将10个
ListItem
加载到一个面板中,并在每个列表面板上设置一个适当的索引

ListItem 1 : lblIndex.value = '#1'
ListItem 2 : lblIndex.value = '#2'
...
ListItem 10 : lblIndex.value = '#10'
如何编写Javascript,每次单击
列表项
上的按钮时,相应的
lblIndex
值都会出现(通过
警报()
)。也就是说,当我单击第二个
列表项
上的
b选项
时,文本“#2”将出现


谢谢

为了方便您的生活,请在编写javascript时使用

考虑到这一点,我们假设您的
ListItem
控件输出一个带有
id
ul
列表。。。确保您有一个类,让我们假设您命名了
列表项
,因此最后您将有:

<div id="ListItem_1" class="list-item">
   ...
</div> 
假设您有10个带有多个按钮的列表,您可以简单地编写:

$(".inside-button").bind("click", function() {
    // a button was clicked, let's do something
});
第1行显示:“Foreach DOM元素,其类名为
inside-button
attachtheclick事件”

在你内心,你想做什么就开枪

根据您的问题,您只想执行列表中的某些操作:

$(this)  // that will be the .inside-button element, so the Button
   .closest(".list-item")  // get me the closest DOM element with a class name of "list-item"
   .find(".text") // find the DOM elemnt with the class name "text"
   .val();  // let's see what value that element has
然后您可以
alert()
it

总而言之:

$(".inside-button").bind("click", function() {
    // a button was clicked, let's do something
    var txt = $(this).closest(".list-item").find("text").val();
    alert( txt );
});
$(this)  // that will be the .inside-button element, so the Button
   .closest(".list-item")  // get me the closest DOM element with a class name of "list-item"
   .find(".text") // find the DOM elemnt with the class name "text"
   .val();  // let's see what value that element has
$(".inside-button").bind("click", function() {
    // a button was clicked, let's do something
    var txt = $(this).closest(".list-item").find("text").val();
    alert( txt );
});