关于jquery中index()函数的问题<body>
<table>
<thead>
<tr>
<th colspan="2">鼠标点击表格项就可以编辑</th>
</tr>
</thead>
<tbody>
<tr>
<th>学号</th>
<th>姓名</th>
</tr>
<tr>
<td id="id">000001</td>
<td id="name">张三</td>
</tr>
<tr>
<td id="id">000002</td>
<td id="name">李四</td>
</tr>
<tr>
<td id="id">000003</td>
<td id="name">王五</td>
</tr>
<tr>
<td>000004</td>
<td>赵六</td>
</tr>
</tbody>
</table>
<input id="btnSave" type="button" value="确定">
</body>
页面很简单,就一个table然后,点击提交的时候
$("#btnSave").click(function(){//按钮点击事件
var str = "";
var users = new Array();
$("table tbody tr").each(function(){
var tr = $(this);
var u = new User();
tr.find("td").each(function(){
var td = $(this);
var att = td.attr("id");
var value = td.html();
alert($(this).index());
if(att=="id") {
str = str + "{\"" + att +"\":\""+value +"\",";
u.id =value;
}
if(att=="name") {
str = str + "\""+att +"\":\""+value +"\" }";
u.name = value;
}
});
if(u.id!="") {
users.push(u);
}
});
str = "{\"users\":[" +str+"]}"; });
function User(id,name) {
this.id = id;
this.name = name;
return this;
} 关键是上条红色的代码,我想找出当前td在当前tr中的索引位置,可是每次都是返回-1$(this).index();不是返回当前元素在同辈元素中的位置的吗?我这样写为什么不对?我应该怎么写呢?jQueryindex()
<table>
<thead>
<tr>
<th colspan="2">鼠标点击表格项就可以编辑</th>
</tr>
</thead>
<tbody>
<tr>
<th>学号</th>
<th>姓名</th>
</tr>
<tr>
<td id="id">000001</td>
<td id="name">张三</td>
</tr>
<tr>
<td id="id">000002</td>
<td id="name">李四</td>
</tr>
<tr>
<td id="id">000003</td>
<td id="name">王五</td>
</tr>
<tr>
<td>000004</td>
<td>赵六</td>
</tr>
</tbody>
</table>
<input id="btnSave" type="button" value="确定">
</body>
页面很简单,就一个table然后,点击提交的时候
$("#btnSave").click(function(){//按钮点击事件
var str = "";
var users = new Array();
$("table tbody tr").each(function(){
var tr = $(this);
var u = new User();
tr.find("td").each(function(){
var td = $(this);
var att = td.attr("id");
var value = td.html();
alert($(this).index());
if(att=="id") {
str = str + "{\"" + att +"\":\""+value +"\",";
u.id =value;
}
if(att=="name") {
str = str + "\""+att +"\":\""+value +"\" }";
u.name = value;
}
});
if(u.id!="") {
users.push(u);
}
});
str = "{\"users\":[" +str+"]}"; });
function User(id,name) {
this.id = id;
this.name = name;
return this;
} 关键是上条红色的代码,我想找出当前td在当前tr中的索引位置,可是每次都是返回-1$(this).index();不是返回当前元素在同辈元素中的位置的吗?我这样写为什么不对?我应该怎么写呢?jQueryindex()
返回当前元素在同辈元素中的位置==》这是对的,你返回-1应该是在同辈元素中存在
在下面这段tr中,我取到第一个td,然后我用 alert($(this).index()); 返回-1,我认为正确值应该是0才对啊同辈元素不是还有个td嘛,为什么会不存在呢?明明是存在的啊<td id="id">000001</td>
<td id="name">张三</td>
If we omit the argument, .index() will return the position of the first element within the set of matched elements in relation to its siblings:上面的引用出自:http://api.jquery.com/index/
If we omit the argument, .index() will return the position of the first element within the set of matched elements in relation to its siblings:上面的引用出自:http://api.jquery.com/index/那不应该返回-1呀,应该返回0才是对的呀现在是两个td呀
<td id="id">000001</td>
<td id="name">张三</td>而我当前的this是第一个td啊 <td id="id">000001</td>
那么这时返回的应该是0吧,怎么会返回-1呢,好奇怪啊
哪位老兄能指点下我这个新手不?
另外,jQuery的each方法会返回索引给你的,不用自己去调用index.
$("tr").each(function(index,elem){
//this === elem
});$.each(obj,function(key,value){})
$.each(array,function(index,elem){});