How to extract values from HTML <input type=“date”> using jQuery

后端 未结 5 1360
无人及你
无人及你 2020-12-16 12:38

Using an HTML input type=\"date\" and a submit button. I would like to populate the variables day, month, and year with t

5条回答
  •  轮回少年
    2020-12-16 13:15

    Date value returned by input type="date" is format yyyy-mm-dd . Could use .split() with argument "-" to retrieve array containing [yyyy, mm, dd]

    Note, alert() expects string as parameter ; does not print values same as console.log() with comma , operator

    var day, month, year;
    
    $('#submit').on('click', function() {
      var date = $('#date-input').val().split("-");
        console.log(date, $('#date-input').val())
      day = date[2];
      month = date[1];
      year = date[0];
      alert(day + month + year);
    });
    

    jsfiddle https://jsfiddle.net/dkxy46ha/2/

提交回复
热议问题