duancong7573 2014-04-26 07:55
浏览 353

使用jquery更改id attr的值

I have a table whose data is fetched from the database, and this table's each td's id is also dynamically unique ( td's id value is equal to database's table value ). In each td, when anyone double click on it, an input field is appeared, when the user edits an input field, I have made an Ajax call (using onblur), and update this td's field in the database. which will be worked I guess, Now I want to change that td's id value, which will come from database (Ajax call).

for example::

My table code ::

<td id="2*Name>Engr._C.F._Zaman" class=" "> Engr. C.F. Zaman  </td>

this td's id is generated from database, where first 2 is id of the table's id, after (*) is the db table's column name [Name] and after (>) is the value of that column's whose id is 2

when anyone click on this td, he/she will get an input field just like as given below::

 <td id="2*Name>Engr._C.F._Zaman" class=" ">
    <input type="text" class="form-control" id="sabbir" value="Engr._C.F._Zaman" name="Name" onblur="AjaxChange('2', 'Name', 'Engr._C.F._Zaman', '2*Name>Engr._C.F._Zaman');">
 </td> 

this is generated using jquery onclick event.

Now if any change of above input field, AjaxChange is called. and my AjaxChange code is ::

function AjaxChange( id,  Attr, tdValue, td ) {  
   $.ajax({
      url: "ajax_call.php",  // this is just update the db 
      type: "POST",
      data: { table: "life", id=id, name=Attr, value=tdValue },  // 
      dataType: "html",
      done: function(data) {
        // first I want to change the id of td, which value will be data
        // td's id format is id*Attr>data

        // next show data in the td with out input field
        // <td id="id*Attr>data"> data </td>
      }
   });
 }

Now How can I change this td's id attribute?

  • 写回答

3条回答 默认 最新

  • dongpiao8821 2014-04-26 08:10
    关注

    I suppose you meant using javascript/jquery, meaning the easiest way to reach the id would be using jqueries "attr"-function.

    Now the question is what selector you should use, I guess in this case that your input fields id ("sabbir") is sufficient enough. You will have to decide this on your own.

    $("#sabbir").closest("td").attr("id", yourNewId);
    

    As answer to the other question in your comment:

    $("#"+yourNewId).html(data);
    

    Should work to replace the tds HTML.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效