javascript - How to access variable values from jstl foreach to jQuery - Stack Overflow

Hello guys this may be easy question but I m finding it quite tricky.Scenario is I m printing all the

Hello guys this may be easy question but I m finding it quite tricky. Scenario is I m printing all the values from the database table using jstl foreach.

<c:forEach items="${pal}" var="p">
    <tr>
        <td><c:out value="${p.pid}"></c:out></td>
        <td><c:out value="${p.pname}"></c:out></td>
        <td><c:out value="${p.pdesc}"></c:out></td>
        <td><c:out value="${p.pestd}"></c:out></td>
        <td><a id="popa" href="#">Allocate</a></td>
    </tr>
</c:forEach>

And now onclick Allocate button I want that particular pid (i.e suppose there are 1,2,3,...,10 pid so when I click on 4th I should get the 4th pid as a value) in jquery.

But in jQuery when I try to get that value I always got last value in the loop whichever button I click.

In jQuery Section

 $("a#popa").click(function(){

     var input = $("<c:out value='${pid}' />");
     alert(input);

 });

and I also tried this

 <c:forEach var='lang' items='${paramValues.pid}'>
     <label><font color='#00CC00'><c:out value='${lang}'/></font></label>
 </c:forEach>

 but nothing works

Please Help me and guide me how to get the current value of pid on click in jQuery.

Hello guys this may be easy question but I m finding it quite tricky. Scenario is I m printing all the values from the database table using jstl foreach.

<c:forEach items="${pal}" var="p">
    <tr>
        <td><c:out value="${p.pid}"></c:out></td>
        <td><c:out value="${p.pname}"></c:out></td>
        <td><c:out value="${p.pdesc}"></c:out></td>
        <td><c:out value="${p.pestd}"></c:out></td>
        <td><a id="popa" href="#">Allocate</a></td>
    </tr>
</c:forEach>

And now onclick Allocate button I want that particular pid (i.e suppose there are 1,2,3,...,10 pid so when I click on 4th I should get the 4th pid as a value) in jquery.

But in jQuery when I try to get that value I always got last value in the loop whichever button I click.

In jQuery Section

 $("a#popa").click(function(){

     var input = $("<c:out value='${pid}' />");
     alert(input);

 });

and I also tried this

 <c:forEach var='lang' items='${paramValues.pid}'>
     <label><font color='#00CC00'><c:out value='${lang}'/></font></label>
 </c:forEach>

 but nothing works

Please Help me and guide me how to get the current value of pid on click in jQuery.

Share Improve this question edited Oct 6, 2015 at 14:59 Fela 27.5k8 gold badges27 silver badges24 bronze badges asked May 31, 2014 at 13:00 AtiqAtiq 1662 gold badges6 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

The value of each id attribute should be unique across the entire DOM. You're using id="popa" in a loop, this means that there will be more than one. When you try to select the id with jQuery it gives you the last one.

I would consider using the following approach. Instead of using an id, use a data-* attribute.

<c:forEach items="${pal}" var="p">
    <tr>
    <td><c:out value="${p.pid}"></c:out></td>
    <td><c:out value="${p.pname}"></c:out></td>
    <td><c:out value="${p.pdesc}"></c:out></td>
    <td><c:out value="${p.pestd}"></c:out></td>
    <td><a data-pid="${p.pid}" href="#">Allocate</a></td>
    </tr>
</c:forEach>

Then you can wire up the click event with something like this.

$("[data-pid]").click(function(){
  var pid = $(this).data("pid");
  alert(pid);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745578901a4634134.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信