C# 使用JavaScript查找具有多个ASP UserControl的ID
我有一个ASP用户控件名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,每次单击列表
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 );
});