สอบถามเรื่อง Select element ครับ

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา สอบถามเรื่อง Select element ครับ

สอบถามเรื่อง Select element ครับ
<select name="testselect" id="testselect">
<option value="1"> บวก 1 ปี</option>
<option value="2">บวก 2 ปี</option>
<option value="3">บวก 3 ปี </option>

<div class="md-form form-group">
<input type="date" name="start_date" id="start_date" class="form-control" required  style="text-align:center;" data-date-format='dd/mm/yyyy' autocomplete="off">
<label for="start_date">วันที่คิดค่าเสื่อม</label>
</div>

<div class="md-form form-group">
<input type="date" name="end_date" id="end_date" class="form-control" required  style="text-align:center;" data-date-format='dd/mm/yyyy' autocomplete="off">
<label for="start_date">วันที่สิ้นสุดคิดค่าเสื่อม</label>
</div>

อยากให้มัน คำนวณหาวันที่ สิ้นสุดคิดค่าเสื่อมครับ

 

เช่น วันที่คิดเสื่อม 2020/8/9
เลือก Select บวก 3 ปี
จะได้วันที่ สิ้นสุดค่า เสื่อม 2023/08/09
ครับ



ิbondsanti 203.172.69.xxx 09-08-2022 15:21:14

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1
ดูเนื้อหานี้เป็นแนวทาง

>>>  อัพเดท >>> 09-08-2022
------------------------------------------
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>

  <select name="testselect" id="testselect">
<option value="1"> บวก 1 ปี</option>
<option value="2">บวก 2 ปี</option>
<option value="3">บวก 3 ปี </option>
  </select>
 
<div class="md-form form-group">
<input type="date" name="start_date" id="start_date" 
       onchange="date_add(this.value,'-','ymd','end_date',null,null,1)"
       class="form-control" required  style="text-align:center;" data-date-format='dd/mm/yyyy' autocomplete="off">
<label for="start_date">วันที่คิดค่าเสื่อม</label>
</div>
 
<div class="md-form form-group">
<input type="date" name="end_date" id="end_date" class="form-control" required  style="text-align:center;" data-date-format='dd/mm/yyyy' autocomplete="off">
<label for="start_date">วันที่สิ้นสุดคิดค่าเสื่อม</label>
</div>
  
<script type="text/javascript">
var date_add = function(date1,separate,format,targetObj,
addDate,addMonth,addYear
){
    var addYear = document.getElementById("testselect").value*1;
    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>   
</body>
</html>

ตัวอย่าง DEMO

บทความแนะนำที่เกี่ยวข้อง
สร้างฟังก์ชั่น javascript บวกวัน เดือน หรือปี อย่างง่ายอ่าน 12,454
ninenik 27.55.68.xxx 09-08-2022






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ