我正在尝试发送多个数据,我点击几个链接背靠背。现在就是这个。
我有一个'分享'链接,如果我点击获取属性
for (int i=0; i<length-1;) {
j = text.indexOf(" ", i);
if(i==j){ //if next space after space, skip it
i = j+1;
continue;
}
if (j==-1) {
j = text.length();
}
System.out.print("\n"+text.substring(i,j));
i = j+1;
}
接下来,单击此链接将打开一个div(popDiv_share),其中列出了数据库中的所有用户。现在,如果我点击任何一个用户,我会使用以下方式获取其属性:
$(".file").click(function(){
var elementFile = $(this);
var id = elementFile.attr("name");
});
现在我所困扰的是,我点击第一个链接,它获得了atrribute。现在当我点击div中的第二个链接时,它也获得了属性,但第一个链接属性消失了。如果连续点击它们之后我如何获得每个链接的两个属性,因为$(this)将仅引用被点击的当前链接而前一个链接将丢失。
任何建议都会有很大的帮助!!
这是我的代码:
$(".userlist").click(function(){
var elementuser = $(this);
var id = elementuser.attr("id");
});
答案 0 :(得分:2)
使用全局变量:
var name;
$(".file").click(function(){
var elementFile = $(this);
name= elementFile.attr("name");
});
$(".userlist").click(function(){
var elementuser = $(this);
var id = elementuser.attr("id");
$.ajax({
type: "POST",
url: "share.php",
data: {
name: name,
id: id
},
success: function(){
}
});
});
或遍历DOM:
$(".userlist").click(function(){
var elementuser = $(this);
var id = elementuser.attr("id");
var name = $(this).closest('#popDiv_share').next('#repos').find('.file').attr("name");;
$.ajax({
type: "POST",
url: "share.php",
data: {
name: name,
id: id
},
success: function(){
}
});
});
答案 1 :(得分:1)
$(".file").click(function(){
var elementFile = $(this);
$(".userlist").click(function(){
var elementUser = $(this);
var name = elementUser.attr("id");
var id = elementFile.attr("name");
var info='name='+name+'&id='+id;
我认为这会起作用
答案 2 :(得分:0)
您无需使用 $.ajax({
type: "POST",
url: "share.php",
data: {
name: name,
id: id
},
success: function(){
}
});
执行类似
fortune(312)
希望它有所帮助:)
答案 3 :(得分:0)
您可以将全局变量附加到window
,以便随时与他们联系:
window.id;
$(".file").click(function(){
var elementFile = $(this);
window.id = elementFile.attr("name");
});
点击第二个链接后,您可以获取该global
变量。
$(".userlist").click(function(){
var id = window.id;
// rest of code
}