简体   繁体   中英

how to reset input value attribute

I'm trying to reset the value of the input once i click a date in the datepicker, my goal is to add a single date in the array once a click a date, what happening is once i click a date for the multiple times, it still holds the previous value of the input.

var date = new Date();
var days = [];

$(document).ready(function(){
  $("#inputMultiDate").datepicker({
    autoclose: true,
    todayHighlight: true,
    multidate: true,
    format: 'dd-mm-yyyy'
  }).on("changeDate", function(){
    dates = $('#inputMultiDate').val().split(',');
    days.push(dates);
     $('#inputMultiDate').attr('value', '');  
    console.log(days);
  });;
});

<input type="text" id="inputMultiDate" class="form-control" placeholder="Pick the multiple dates">

You can easily access the event by using datepicers onSelect:

 var date = new Date(); var days = []; $(document).ready(function() { $("#inputMultiDate").datepicker({ autoclose: true, todayHighlight: true, multidate: true, format: 'dd-mm-yyyy', onSelect: function(dateText) { dates = $('#inputMultiDate').val().split(','); days.push(dates); $('#inputMultiDate').attr('value', ''); console.log(days); let value = $("#inputMultiDate").datepicker("getDate"); console.log(value); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <input type="text" id="inputMultiDate" class="form-control" placeholder="Pick the multiple dates">

$('#inputMultiDate').val('')

In jQuery "val" is a get function also a set function. You can set your input with val('').

Try to call the new Date() function every time the date changes. Currently, you have one date as the page loads.

var date = new Date();
var days = [];

$(document).ready(function(){
  $("#inputMultiDate").datepicker({
    autoclose: true,
    todayHighlight: true,
    multidate: true,
    format: 'dd-mm-yyyy'
   }).on("changeDate", function(){
    date = new Date();
    dates = $('#inputMultiDate').val().split(',');
    days.push(dates);
    $('#inputMultiDate').attr('value', '');  
    console.log(days);
  });;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM