今天準(zhǔn)備把選定表格的其中一行的數(shù)據(jù)通過(guò)ajax傳給后端,但是網(wǎng)站確崩潰了。
代碼如下:
$('.icon-edit').click(function (event) { 這是一個(gè)按鈕 o=$('.icon-edit').index($(this))+1; edit.style.display='block'; //console.log('$(this)',$(this).parent().parent()); let message=$(this).parent().parent(); $("#non").val(message.children('td:eq(0)').html()); $("#name").val(message.children('td:eq(1)').html()); $("#sex").val(message.children('td:eq(2)').html()); $("#age").val(message.children('td:eq(3)').html()); $("#xueyuan").val(message.children('td:eq(4)').html()); $("#grade").val(message.children('td:eq(5)').html()); $("#phone").val(message.children('td:eq(6)').html()); $("#room").val(message.children('td:eq(7)').html()); l=message.children('td:eq(0)').html(); }); $('#ok').click(function () { //event.stopImmediatePropagation(); let text=$('table'); id=$('#non').val(); username=$('#name').val(); sex=$('#sex').val(); age=$('#age').val(); institute=$('#xueyuan').val(); grade=$('#grade').val(); phone=$('#phone').val(); hlbhl=$('#room').val() text.find("tr:eq("+o+")").children('td:eq(0)').text(id); text.find("tr:eq("+o+")").children('td:eq(1)').text(username); text.find("tr:eq("+o+")").children('td:eq(2)').text(sex); text.find("tr:eq("+o+")").children('td:eq(3)').text(age); text.find("tr:eq("+o+")").children('td:eq(4)').text(institute); text.find("tr:eq("+o+")").children('td:eq(5)').text(grade); text.find("tr:eq("+o+")").children('td:eq(6)').text(phone); text.find("tr:eq("+o+")").children('td:eq(7)').text(hlbhl); $.ajax({ type: "POST", url: "doAction2.php",//請(qǐng)求的后臺(tái)地址 data: { non:o, id: id, username: username, sex: sex, age: age, institute: institute, grade: grade, phone: phone, hlbhl: hlbhl },//前臺(tái)傳給后臺(tái)的參數(shù) dataType: "json", ansync: true, ContentType: "application/json; charset=utf-8", success: function (msg) {//msg:返回值 a=2; console.log(a); } }); edit.style.display='none'; });
代碼的大意是我點(diǎn)擊一個(gè)按鈕($('.icon-edit'))然后彈出一個(gè)表單(edit),表單是數(shù)據(jù)來(lái)源于點(diǎn)擊,然后修改表格
的內(nèi)容點(diǎn)擊確定按鈕($('#ok'))后把表單數(shù)據(jù)覆蓋掉之前點(diǎn)擊行的數(shù)據(jù),達(dá)到修改表格的目的,點(diǎn)擊確定時(shí)觸發(fā)ajax,
把修改后的數(shù)據(jù)發(fā)送給后端,拿到數(shù)據(jù)并更新數(shù)據(jù)庫(kù)。
結(jié)果頁(yè)面不報(bào)錯(cuò),而是直接崩潰了,查看了許久,才發(fā)現(xiàn)是由于ajax的data參數(shù)寫錯(cuò)了,之前寫成這樣:
id=text.find("tr:eq("+o+")").children('td:eq(0)').text(id); username=text.find("tr:eq("+o+")").children('td:eq(1)').text(username); sex=text.find("tr:eq("+o+")").children('td:eq(2)').text(sex); age=text.find("tr:eq("+o+")").children('td:eq(3)').text(age); institute=text.find("tr:eq("+o+")").children('td:eq(4)').text(institute); grade=text.find("tr:eq("+o+")").children('td:eq(5)').text(grade); phone=text.find("tr:eq("+o+")").children('td:eq(6)').text(phone); hlbhl=text.find("tr:eq("+o+")").children('td:eq(7)').text(hlbhl); $.ajax({ type: "POST", url: "doAction2.php",//請(qǐng)求的后臺(tái)地址 data: { non:o, id: id, username: username, sex: sex, age: age, institute: institute, grade: grade, phone: phone, hlbhl: hlbhl },//前臺(tái)傳給后臺(tái)的參數(shù) dataType: "json", ansync: true, ContentType: "application/json; charset=utf-8", success: function (msg) {//msg:返回值 a=2; console.log(a); } }); edit.style.display='none'; });
從上面可以看出,我傳給data的數(shù)據(jù)并不是字符串之類的,而是一個(gè)n.fn.init [td, prevObject: n.fn.init(1), context: document],
由于自己的粗心和對(duì)導(dǎo)致ajax出現(xiàn)錯(cuò)誤的情況了解比較少,導(dǎo)致看了很久的代碼才發(fā)現(xiàn)原因,剛開(kāi)始就以為不會(huì)是參數(shù)導(dǎo)致,
相信看了本文案例你已經(jīng)掌握了方法,更多精彩請(qǐng)關(guān)注Gxl網(wǎng)其它相關(guān)文章!
推薦閱讀:
ES6實(shí)現(xiàn)全屏滾動(dòng)插件步驟詳解
Vue中watch使用方法總結(jié)
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com