In my JSP page ,i want to dynamically create a label, which is inside a achor tag. when i clik another anchor tag , i want to get corresponding label text using javascript or jquery.
在我的JSP页面中,我想动态创建一个标签,该标签位于achor标签内。当我clik另一个锚标签时,我想使用javascript或jquery获取相应的标签文本。
This is my JSP page code.
这是我的JSP页面代码。
<body>
<%
for (int i = 0; i < sam.groupName.length; i++) {
out.print("<a href='#'>");
out.print("<img src='twoMen.jpg'/>");
out.print("<label for='groupsId' id='labelId"+i+"'>"+ sam.groupName[i] + "</label>");
out.print("</a>");
out.print("<a href='javascript:void(0);' onclick='validation(this,"+i+");'>Edit</a>");
}
%>
</body>
Javascript code:
function validation(anchor,i) {
alert("Label value="+????);
}
3 个解决方案
#1
1
Quick answer:
function validation(anchor,i) {
var value = $("#labelId"+i).text();
alert("Label value= "+ value);
}
Just use the ID of the label you have, then get the text from it.
只需使用您拥有的标签的ID,然后从中获取文本。
.text()
will return to you the TEXT that is in the tag. Not if that text is contained within another nested tag. If that is the case, use .html()
or specify your jQuery selector.
.text()将返回标记中的TEXT。如果该文本包含在另一个嵌套标记中,则不会。如果是这种情况,请使用.html()或指定您的jQuery选择器。
UPDATE
I also thought of another solution, this one will be simpler I think:
我还想到了另一个解决方案,我认为这个更简单:
<body>
<%
for (int i = 0; i < sam.groupName.length; i++) {
out.print("<a href='#'>");
out.print("<img src='twoMen.jpg'/>");
out.print("<label for='groupsId' id='labelId"+i+"'>"+ sam.groupName[i] + "</label>");
out.print("</a>");
out.print("<a href='javascript:void(0);' onclick='validation(this,'"+sam.groupName[i]+"');'>Edit</a>");
}
%>
</body>
then you can use the validation function as follows:
那么您可以使用验证功能,如下所示:
function validation(anchor,value) {
alert("Label value="+ value);
}
This does the same, but it passes the value you need instead of the index pointing to the value you need. This works UNLESS you need the i index for anything else later on.
这样做也是如此,但它传递了您需要的值,而不是指向您需要的值的索引。这是有效的,除非您以后需要i索引。
#2
0
Give some class to anchor's say class1, and use live method of jquery as,
给一些类锚定说class1,并使用jquery的live方法,
$(".class1").live("click",function(){
alert(this.html())
});
#3
0
In these cases the best thing is look at how the final code is rendered. However, you can take it from the ID:
在这些情况下,最好的方法是查看最终代码的呈现方式。但是,您可以从ID中获取它:
var myLabelId = 'labelId'+i;
var myLabel = document.getElementById(myLabelId);
var myLabelValue = myLabel.innerText;
#1
1
Quick answer:
function validation(anchor,i) {
var value = $("#labelId"+i).text();
alert("Label value= "+ value);
}
Just use the ID of the label you have, then get the text from it.
只需使用您拥有的标签的ID,然后从中获取文本。
.text()
will return to you the TEXT that is in the tag. Not if that text is contained within another nested tag. If that is the case, use .html()
or specify your jQuery selector.
.text()将返回标记中的TEXT。如果该文本包含在另一个嵌套标记中,则不会。如果是这种情况,请使用.html()或指定您的jQuery选择器。
UPDATE
I also thought of another solution, this one will be simpler I think:
我还想到了另一个解决方案,我认为这个更简单:
<body>
<%
for (int i = 0; i < sam.groupName.length; i++) {
out.print("<a href='#'>");
out.print("<img src='twoMen.jpg'/>");
out.print("<label for='groupsId' id='labelId"+i+"'>"+ sam.groupName[i] + "</label>");
out.print("</a>");
out.print("<a href='javascript:void(0);' onclick='validation(this,'"+sam.groupName[i]+"');'>Edit</a>");
}
%>
</body>
then you can use the validation function as follows:
那么您可以使用验证功能,如下所示:
function validation(anchor,value) {
alert("Label value="+ value);
}
This does the same, but it passes the value you need instead of the index pointing to the value you need. This works UNLESS you need the i index for anything else later on.
这样做也是如此,但它传递了您需要的值,而不是指向您需要的值的索引。这是有效的,除非您以后需要i索引。
#2
0
Give some class to anchor's say class1, and use live method of jquery as,
给一些类锚定说class1,并使用jquery的live方法,
$(".class1").live("click",function(){
alert(this.html())
});
#3
0
In these cases the best thing is look at how the final code is rendered. However, you can take it from the ID:
在这些情况下,最好的方法是查看最终代码的呈现方式。但是,您可以从ID中获取它:
var myLabelId = 'labelId'+i;
var myLabel = document.getElementById(myLabelId);
var myLabelValue = myLabel.innerText;