jQuery+ajax實現按一下滑鼠修改內容的方法_jquery

來源:互聯網
上載者:User

現有表格中的一行的代碼如下所示: 

<tr> <td><span class="catid">2</span></td> <td>公司介紹</td> <td>內部欄目</td> <td><span class="listorder" title="點擊修改">2</span></td></tr>

要實現按一下滑鼠修改內容思路如下:
 
1、點擊欄目排序欄目中的數字,擷取同一行的第一列中的內容,即欄目id
2、隱藏欄目排序中的數字
3、在欄目排序列中插入input框,並在input框中顯示欄目排序中的內容,並設定為焦點
4、修改input中的內容,失去焦點的時候提交資料,用ajax向伺服器傳遞資料 方法為post方法
5、提交資料的時候,友好提示修改中。。。 或者等待圖片
6、返回成功資訊 ,重新顯示修改後的內容 去掉input框

實現這一功能的jquery核心代碼如下:

$('.listorder').click(function(e){ var catid = $(this).parent().siblings("td:eq(0)").text();//擷取同一行上 第一列中的id值 var listorder_now_text = $(this).text();//擷取listorder中的內容 先儲存起來 $(this).text("");//設定內容為空白 var list_form = '<input type="text"  value="'+listorder_now_text+'" size=2 class="listorder_input" />' ; $(this).parent().append(list_form); //插入 input框 $(".listorder_input").focus();//自訂一個div 提示修改中 var loading = '<div id="loading"><img src="img/loading.gif" alt="修改中..."/></div>'; $(this).parent().append(loading); $('#loading')  .css({   "color" : "red" ,   "display" : "none"  })//定義ajax的全域事件 $(this).ajaxStart(function(){  $('#loading').show(); }) $(this).ajaxStop(function(){  $('#loading').remove(); }) $(".listorder_input").blur(function(){  var thislist = $(this).siblings(); //取得同級的標籤 即 修改後需要顯示的 listorder  $.post("ajax.php",{  action : "mod_listorder",  catid : catid ,  listorder : $(this).attr("value")  } , function(data, textStatus){    $(thislist).text(data);    }  );//end .post  $(this).remove(); })//end function blur})// end function click

ajax.php中內容就簡單了,這裡只做處理做示範用,並沒有向伺服器提交資料,代碼如下:

sleep(1);//延時運行1秒,查看效果用,實際代碼中不需要echo $_POST['listorder'];
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.