2012-08-25 55 views
0

过去两天使用jquery iam尝试获取通过调用ajax.i通过调用servlet执行更新所需的单元格值使用了双击事件用文本框替换单元格以编辑值,当文本框失去焦点时,将值传递给servlet.i需要传递编辑后的值以及用于更新表的ID号。现在,我需要获取行的id值用户编辑的内容。传递单元格值以便使用ajax更新表格

我试图获取值,但只有第一行得到更新,因为我只能得到第一行的id值。如何获取其他行的值,以便用户可以更新所有行。

<script> 
     $(function(){ 
    $('.table-cell').dblclick(function(){ 
// if ($(this).attr('editing') == '0') 
// { 
    // only trigger this when the cell is not in edit mode 
    $(this).attr('editing',1); 

    // show the input 
    var input = $('<input type="text" value="'+$(this).html()+'">'); 
    $(this).html(''); 
    $(this).append(input); 

    // bind blur event to input 
    input.blur(function(){ 

     // update value from input to the table cell 
     var value = $(this).val(); 
     // alert(value); 
     $(this).parent().text(value); 
     $(this).parent().attr('editing',0); 
     //this will get only the first id value,no matter which column i edit 
     var fid= document.getElementById("filename").textContent; 
     // alert(fid); 
     //this will iterate 
    $('#table').ready(function() { 
     $(this).find('.filename').each(function() { 
      alert($(this).html()); 
     }) 
    }) 




     // send the data to the server for update 
     $.ajax({ 
      url: 'update', 
      data: {text1:value,filename1:fid}, 
      type:"POST", 
      dataType:"json", 
      error:function(data){ 
       // alert('error') 
      }, 
      success: function(data) { 
       //alert('updated!') 
      } 
     }); 

    }); 
// } 
}); 
    }); 
</script> 
<table border="1" id="table"> 
<th>S.NO</th> 
<th>Description</th> 
<th>File Name</th> 
<th>Category</th> 
<th>Sector</th> 
<th>Delete</th> 
<th>Edit</th> 
<c:forEach items="${fileDetails}" var="item" varStatus="counter"> 
<tr> 
<td>${counter.count}</td> 
<td onDblclick="javascript:doubleclick(this);" >${item.filedesc}</td> //editable row 
<td><a href="download?name=${item.filepath}" id="changes">${item.filenaame}</a></td> 
<td>${item.category}</td> 
<td>${item.sector}</td> 
<td hidden="true" id="filename" class="filename" index="1" >${item.id}</td> //id values from db table 
<td> <input type="submit" onclick="filedelete('${item.id}','${item.sector}')" value="Delete"> </td> 

</tr> 

</c:forEach> 
<input type="hidden" name="delete" id="delete" > 
<input type="hidden" name="num" id="num"> 
<input type="hidden" name="sect" id="sect"> 

</table> 

回答

0

可以编码行数,例如像:

<tr index="counter.index"> 

...比在适当的地方

row=$(this).parent().attr("index") 
+0

IAM弄个的jQuery越来越我的ID值表格数据库通过这个 $ {item.id}计数器只显示行数,它不是从数据库派生的 – ksa

+0

难道你不能然后将该行的属性设置为item.id? – kwicher

+0

我做了,但它不工作,没有来自数据库的ID值,我不能更新 – ksa