简体   繁体   English

在Razor Foreach循环中使用jquery获取文本框的值

[英]Get value of textbox using jquery inside of Razor Foreach loop

I have a div like this in my view, keep in mind this is inside of a @foreach loop in my razor view: 我的视图中有一个这样的div,请记住,这在我的剃刀视图中的@foreach循环内:

<div class="testClass">
    <input type="text" class="tempLabel" /> 
                    <input type="button" data-value=@(item.ID) class="testButton3" value="Test Alert" />
</div>

This javascript works: 这个javascript可以运作:

$(".testButton3").click(function () {    
     alert($(this).attr('data-value'));                
        });

what I want to do is get the value of the textbox inside of my tempClass class when I click the above button. 当我单击上面的按钮时,我想做的就是获取tempClass类中文本框的值。 I tried this but it does not work: 我试过了,但是不起作用:

$(".testButton3").click(function () {    
         alert($(".tempLabel").($(this).val()));               
            });

Is this possible? 这可能吗? Perhaps it is not possible to get the value of the textbox inside a foreach loop.... 也许不可能在foreach循环中获取文本框的值。

They might be multiple elements created by foreach. 它们可能是由foreach创建的多个元素。 you can try .prev() method of jquery to reach exact textbox like below. 您可以尝试使用jquery的.prev()方法到达如下所示的确切文本框。

$(".testButton3").click(function () {    
     alert($(this).prev(".tempLabel").val();                
        });

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

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