ปัญหาการใช้ datepicker ใน jqueryui กรณี clone input แล้ว
datepicker ใช้งานไม่ได้ เนื่องจาก มีการ gen ค่า id ของ input ซ้ำกัน
ตั้งแต่มีการเรียกใช้ วิธีการแก้ปัญหา
- กำหนด id เป็นค่าว่าง
- ยกเลิก css hasDatepicker ที่ได้จากการ clone
- ยกเลิกการจัดการทั้งหมด ที่ได้มาจากตัว clone
- เรียกใช้ datepicker() ใหม่อีกครั้ง
ตัวอย่าง
โค้ด คำอธิบายแสดงในโค้ด
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <head> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" /> <title></title> <script src= "//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" ></script> <?php $jquery_ui_v = "1.8.5" ; $theme = array ( "0" => "base" , "1" => "black-tie" , "2" => "blitzer" , "3" => "cupertino" , "4" => "dark-hive" , "5" => "dot-luv" , "6" => "eggplant" , "7" => "excite-bike" , "8" => "flick" , "9" => "hot-sneaks" , "10" => "humanity" , "11" => "le-frog" , "12" => "mint-choc" , "13" => "overcast" , "14" => "pepper-grinder" , "15" => "redmond" , "16" => "smoothness" , "17" => "south-street" , "18" => "start" , "19" => "sunny" , "20" => "swanky-purse" , "21" => "trontastic" , "22" => "ui-darkness" , "23" => "ui-lightness" , "24" => "vader" ); $jquery_ui_theme = $theme [14]; ?> <link type= "text/css" rel= "stylesheet" href= "//ajax.googleapis.com/ajax/libs/jqueryui/<?=$jquery_ui_v?>/themes/<?=$jquery_ui_theme?>/jquery-ui.css" /> <script src= "//ajax.googleapis.com/ajax/libs/jqueryui/<?=$jquery_ui_v?>/jquery-ui.min.js" ></script> <style type= "text/css" > html,body{ padding:0px; margin:0px; } body{ font-size:12px; } div.test_demo_main{ margin:auto; width:75%; } .ui-datepicker{ /* width:150px; */ font-family:tahoma; font-size:11px; text-align:center; } </style> </head> <body> <div class = "test_demo_main" > <br /> <br /> <form id= "form1" name= "form1" method= "post" action= "" > <table id= "myTbl" width= "650" border= "1" cellspacing= "2" cellpadding= "0" > <tr class = "firstTr" > <td width= "119" > <select name= "data1[]" id= "data1[]" > <option value= "" >Please select</option> <option value= "1" >data1</option> <option value= "2" >data2</option> </select></td> <td width= "519" > <input name= "h_item_id[]" type= "hidden" id= "h_item_id[]" value= "" /> <input type= "text" class = "text_data datepicker" name= "data2[]" value= "" /> </td> </tr> </table> <br /> <table width= "500" border= "0" cellspacing= "0" cellpadding= "0" > <tr> <td> <button id= "addRow" type= "button" >+</button> <button id= "removeRow" type= "button" >-</button> <input name= "h_all_id_data" type= "hidden" id= "h_all_id_data" value= "" /> <input type= "submit" name= "Submit" id= "Submit" value= "Submit" /></td> </tr> </table> </form> </div> <script type= "text/javascript" > $( function (){ $( ".datepicker" ).datepicker(); $(document.body).on( "click" , "#addRow" , function (){ // ส่วนของการ clone ข้อมูลด้วย jquery clone() ค่า true คือ // การกำหนดให้ ไม่ต้องมีการ ดึงข้อมูลจากค่าเดิมมาใช้งาน // รีเซ้ตเป็นค่าว่าง ถ้ามีข้อมูลอยู่แล้ว ทั้ง select หรือ input $( ".firstTr:eq(0)" ). clone (true) .find( "input" ).attr( "value" , "" ). end () .find( "select" ).attr( "value" , "" ). end () // .appendTo($( "#myTbl" )); var lastIndex=$( ".datepicker" ).size()-1; // หา index ของตัว input ล่าสุด $( ".datepicker:eq(" +lastIndex+ ")" ) .attr( "id" , "" ) // - กำหนด id เป็นค่าว่าง .removeClass( 'hasDatepicker' ) // - ยกเลิก css hasDatepicker ที่ได้จากการ clone .unbind() // - ยกเลิกการจัดการทั้งหมด ที่ได้มาจากตัว clone .datepicker(); // - เรียกใช้ datepicker() ใหม่อีกครั้ง }); $( "#removeRow" ).click( function (){ // // ส่วนสำหรับการลบ if ($( "#myTbl tr" ).size()>1){ // จะลบรายการได้ อย่างน้อย ต้องมี 1 รายการ $( "#myTbl tr:last" ).remove(); // ลบรายการสุดท้าย } else { // เหลือ 1 รายการลบไม่ได้ alert( "ต้องมีรายการข้อมูลอย่างน้อย 1 รายการ" ); } }); }); </script> </body> </html> |