1. 程式人生 > >django 用戶管理(3)--編輯用戶 and 修改用戶密碼

django 用戶管理(3)--編輯用戶 and 修改用戶密碼

ons == 服務器 .get 網頁 checked his === arr

編輯用戶

1、點擊編輯按鈕
技術分享圖片

流程:
(1)、禁用編輯的herf,避免跳轉到其他網頁
href="javascript:void(0)"

(2)、需要給“編輯”按鈕添加class 為btn-update-user
(3)、編輯js ===》點擊編輯按鈕時 發生的事件為:1、將id傳給服務器。2、返回數據顯示 ===》ajax
先取得id的值
var id = jQuery(this).attr(‘update-id‘);

var form = jQuery(‘.form-update-user‘); #定義找到from表單

設置id值,找到name=id的,並將value=result[‘result‘][‘id‘]

form.find(‘[name=id]‘).val(result[‘result‘][‘id‘]);

if (result[‘result‘][‘sex‘]) {
form.find(‘[name=sex][value=1]‘).prop(‘checked‘, true);
}
else {
form.find(‘[name=sex][value=0]‘).prop(‘checked‘, true);
}
(result[‘result‘][‘sex‘] =1時(為男),則為true ,此時找到元素 [name=sex][value=1] ,prop是將‘checked‘ 設置為true,表示顯示男的按鈕,為false,則不顯示為男的按鈕

技術分享圖片

以上是點擊編輯按鈕獲取的值。

2、點擊保存
將數據通過ajax的方式傳送給服務器,並返回結果到前端
var data = jQuery(‘.form-update-user‘).serializeArray();
console.log(data);
jQuery.post("{% url ‘user:update_ajax‘ %}", data, function (result) {
這裏就寫if result[‘code‘] == 200 400 403

                    },‘json‘);

jQuery.post jQuery.get 區別:
jQuery.get:將所有信息明文傳輸到服務器上,不安全,速度快,有緩存

jQuery.post:將密文傳輸,速度慢,安全

django 用戶管理(3)--編輯用戶 and 修改用戶密碼