简体   繁体   English

如何在Javascript代码中使用JSP的值?

[英]How can I use a value of JSP in Javascript code?

I have a list of elements, each of them appeared in my page through jsp. 我有一个元素列表,每个元素都通过jsp出现在我的页面中。 So I have this: 所以我有这个:

<div id="title"><%= list.get(ind).getTitle() %></div>

When I display the elements, as this tag is in for loop, I get everything right. 当我显示元素时,因为此标记位于for循环中,所以我一切正常。 Now I what to place a button for deleting each of this elements. 现在,我要放置一个按钮来删除每个元素。 What I have done is: 我所做的是:

<input type="submit" name="submit" onclick="deleteNewsFunction();" id="submit_btn"/>

In the deleteNewsFunction() I just want to print the data. 在deleteNewsFunction()中,我只想打印数据。

<script>
  function deleteNewsFunction(){
       var item = $('#title').val();
       alert(item);
  }
</script>

The problem is that the title var gets always the first item even if I am clicking on the button of the second item. 问题是,即使我单击第二个项目的按钮,标题var也会始终获得第一个项目。 I was thinking that maybe a solution is to add the "list.get(ind).getTitle()" as a parameter in the function like deleteNewsFunction(<%=list.get(ind).getTitle()%>) but then it did not work at all. 我在想,也许解决方案是在像deleteNewsFunction(<%= list.get(ind).getTitle()%>)之类的函数中添加“ list.get(ind).getTitle()”作为参数。它根本没有用。

Does anyone know how to help me? 有人知道如何帮助我吗?

Thanks a lot! 非常感谢!

i would suggest the following: 我建议以下内容:

<div id="title<%=ind%>"><%= list.get(ind).getTitle() %></div>

then 然后

<input type="submit" name="submit" onclick="deleteNewsFunction(<%=ind%>);" id="submit_btn"/>

finally: 最后:

<script>
  function deleteNewsFunction(ind){
       var item = $('#title' + ind).val();
       alert(item);
  }
</script>

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

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