jquery combines Ajax to double-click a table table to make the table editable and saved to the database

Source: Internet
Author: User

Recently in the project, to achieve by double-clicking the table Table of the TR, so that the table row can be editable, to achieve the ability to modify data and save to the database, no need to say, directly paste the code bar. I hope I can get my colleagues to correct you.

1 functionTdedit (element, id) {2     vari_a = "<input class= ' edit_td ' type= ' text ' style= ' height:30px; width:40px; ' value= ' ";3     varI_b = "'/>";4     varT_a = "<textarea class= ' tarea ' cols= '" rows= ' 3 ' style= ' width:90% ' > ";5     varT_b = "</textarea>";6     varTD = $ (Element). FIND ("TD");7     if(Td.length > 0) {8         varsc = $ (Element). Children (). EQ (1). text ();9         varSS = $ (Element). Children (). EQ (2). text ();Ten         varSequence = $ (Element). Children (). EQ (3). text (); One         varWeight = $ (Element). Children (). EQ (4). text (); A         varMax = $ (Element). Children (). EQ (5). text (); -         varMin = $ (Element). Children (). EQ (6). text (); -         varCV = $ (Element). Children (). EQ (7). text (); the         varExplain = $ (element). Children (). EQ (8). text (); -  -$ (Element). Children (). EQ (1). HTML ($ (t_a + SC +t_b)); -$ (Element). Children (). EQ (2). HTML ($ (t_a + SS +t_b)); +$ (Element). Children (). EQ (3). HTML ($ (i_a + sequence + "' id= ' num1" +i_b)); -$ (Element). Children (). EQ (4). HTML ($ (i_a + weight + "' id= ' num2" +i_b)); +$ (Element). Children (). EQ (5). HTML ($ (i_a + max + "' id= ' MaxValue" +i_b)); A$ (Element). Children (). EQ (6). HTML ($ (i_a + min + "' id= ' MinValue" +i_b)); at$ (Element). Children (). EQ (7). HTML ($ (t_a + CV +t_b)); -$ (Element). Children (). EQ (8). HTML ($ (t_a + explain +t_b)); -     } -$ (". Edit_td"). Click (function () { -         return false; -     }); in$ (". Tarea"). Click (function () { -         return false; to     }); +     //Get Focus -$ (". Edit_td"). Trigger ("Focus"); the$ (". Tarea"). Trigger ("Focus"); *  $     //The text box loses focus, commits the content, and becomes text againPanax Notoginseng$ (". Save"). Click (function () { -         //verification Information "n":/^\d+$/ the         varReg =/^[0-9]+\. {0,1} [0-9] {0,2}$/; +         varNUM1 = $ ("#num1"). Val (); A         varnum2 = $ ("#num2"). Val (); the         varMax = $ ("#maxvalue"). Val (); +         varMin = $ ("#minvalue"). Val (); -         if(parseint (min) >parseint (max)) { $Alert ("Minimum value cannot be greater than maximum!") ");  $             return false; -         } -         if(!reg.test (NUM1) | |!reg.test (NUM2) | |!reg.test (max) | |reg.test (min)) { theAlert ("Please enter a number!"));  -             return false;Wuyi         } the         //re-assign the new value -$ (". Edit_td"). each (function(i) { Wu             varNewtxt = $ ( This). Val (); -$ (Element). Children (). EQ (i + 3). HTML (newtxt); About         }); $$ (". Tarea"). each (function(j) { -             varNewtarea = $ ( This). Val (); -             if(J < 2) { -$ (Element). Children (). EQ (j + 1). HTML (newtarea); A             } +             Else { the$ (Element). Children (). EQ (j + 5). HTML (newtarea); -             } $         }); the  the         varNEW_SC = $ (Element). Children (). EQ (1). text (); the         varNEW_SS = $ (Element). Children (). EQ (2). text (); the         varNew_sequence = $ (Element). Children (). EQ (3). text (); -         varNew_weight = $ (Element). Children (). EQ (4). text (); in         varNew_max = $ (Element). Children (). EQ (5). text (); the         varNew_min = $ (Element). Children (). EQ (6). text (); the         varNEW_CV = $ (Element). Children (). EQ (7). text (); About         varNew_explain = $ (Element). Children (). EQ (8). text (); the         if(new_sc! = SC | | NEW_SS! = SS | | new_sequence! = Sequence | | New_weight! = Weight | | New_max! = MAX | | new_min! = min | | n EW_CV! = CV | | New_explain! =explain) { the $.ajax ({ theType: ' POST ', +ContentType: ' Application/json ', -URL: '/ajax/ajaxaction.ashx/updateruledetail ', theData: ' {ID: ' + ID + ' ", Strcon:" ' + New_sc + ' ", Strstandard:" ' + New_ss + ' ", Sequence:" ' + new_sequence + ' ", Weight:" ' + N Ew_weight + ' ", Candidatevalue:" 'Bayi+ NEW_CV + ' ", MaxValue:" ' + New_max + ' ", MinValue:" ' + New_min + ' ", Explain:" ' + New_explain + ' "} ', theDataType: ' JSON ', theAsynctrue, -Beforesend:function () { -                 }, theSuccessfunction(data) { theAlert ("Save success!") "); theWindow.location.reload ();//Refresh Page the                 }, -Errorfunction(XMLHttpRequest, Textstatus, Errorthrown) { theAlert (XMLHttpRequest + ': ' +textstatus); the window.location.reload (); the                 }94             }); the         } the         Else { theAlert ("Warm tip: You haven't made any changes!") ");98 window.location.reload (); About         } -         101     });102}
JS

Front Page bindings:

1 <tr ondblclick= "Tdedit (this, @item. ID)" ></tr>
Html

Eventually:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.