javascript บวกวันที่อัตโนมัติตามช่องที่กรอก textbox
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา javascript บวกวันที่อัตโนมัติตามช่องที่กรอก textbox
javascript บวกวันที่อัตโนมัติตามช่องที่กรอก textbox
ขอสอบถามหน่อยค่ะ ไม่รู้จะเขียนโค้ด JS ยังไง ไม่ค่อยถนัด อยากให้แสดงวันที่อัตโนมัติ จากวันที่ปัจจุบันแล้วบวกไปอีก ตามที่กรอกใน Textbox เช่น ตามตัวอย่างกรอก 3 ระบบก็จะแสดงในวันครบกำหนด เป็น 28-09-2017 ค่ะ

จะทำยังไงค่ะ
จะทำยังไงค่ะ

คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
ความคิดเห็นที่
1
ดูเนื้อหานี้เป็นแนวทาง
>>> อัพเดท >>> 25-09-2017
------------------------------------------
ตัวอย่าง
สร้างฟังก์ชั่น javascript บวกวัน เดือน หรือปี อย่างง่าย
>>> อัพเดท >>> 25-09-2017
------------------------------------------
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 | <script> var date_add = function (date1,separate,format,targetObj, addDate,addMonth,addYear ){ separate = typeof separate !== 'undefined' ? separate : '-' ; format = typeof format !== 'undefined' ? format : 'dmy' ; targetObj = typeof targetObj !== 'undefined' ? targetObj : null; addDate = typeof addDate !== 'undefined' ? addDate : null; addMonth = typeof addMonth !== 'undefined' ? addMonth : null; addYear = typeof addYear !== 'undefined' ? addYear : null; var s_date=date1; var arr_s_date = s_date.split(separate); var new_s_date = arr_s_date[format.indexOf( 'm' )] + '/' +arr_s_date[format.indexOf( 'd' )] + '/' +arr_s_date[format.indexOf( 'y' )]; var e_date = new Date (new_s_date); if (addDate){ e_date.setDate(e_date. getDate () + addDate); } if (addMonth){ e_date.setMonth(e_date.getMonth() + addMonth); } if (addYear){ e_date.setFullYear(e_date.getFullYear() + addYear); } function pad(s) { return (s < 10) ? '0' + s : s; } var new_t_date = []; new_t_date[0]=(format.charAt(0)== 'd' )?pad(e_date. getDate ()): (format.charAt(0)== 'm' )?pad(e_date.getMonth()+1):e_date.getFullYear(); new_t_date[1]=(format.charAt(1)== 'd' )?pad(e_date. getDate ()): (format.charAt(1)== 'm' )?pad(e_date.getMonth()+1):e_date.getFullYear(); new_t_date[2]=(format.charAt(2)== 'd' )?pad(e_date. getDate ()): (format.charAt(2)== 'm' )?pad(e_date.getMonth()+1):e_date.getFullYear(); var returnValue=[new_t_date[0],new_t_date[1],new_t_date[2]].join(separate); if (targetObj){ document.getElementById(targetObj).value = returnValue; } else { return returnValue; } return [new_t_date[0],new_t_date[1],new_t_date[2]].join(separate); } </script> <input name= "a" id= "id_a" value= "25-09-2017" /><br> <input type= "number" name= "b" id= "id_b" value= "0" min= "0" /><br> <input name= "c" id= "id_c" /> <script> $( function (){ $( "#id_b" ).on( "input" , function (){ var startDate = $( "#id_a" ).val(); var dataAdd = parseInt($(this).val()); var resultDate = date_add(startDate, '-' , 'dmy' , 'id_c' ,dataAdd,null,null); }); }); </script> |
ตัวอย่าง

ขอบคุณทุกการสนับสนุน
![]()