简体   繁体   中英

How to check if input type=“date” has value

I have a date input and I am trying to check if has value or not, but the else condition is always displayed. How I can check if my date input has value?

<input name="bike_model_desc" class="date-theft" placeholder="DD/MM/AAAA" id="bike_acq_date_id" type="date" required>

$("#bike_acq_date_id").change(() => {
    if ($("#bike_acq_data_id").val()) {
        console.log("have value")
    } else {
        console.log("don't have value")
    }
})

Actually your id is bike_acq_date_id not bike_acq_data_id . But there is no need to use id to select datepicker value.

Use parameter of call back function of change event: param.target.value

 $("#bike_acq_date_id").change((a) => { if (a.target.value) { console.log("have value",a.target.value) } else { console.log("don't have value") } })
 <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <input name="bike_model_desc" class="date-theft" placeholder="DD/MM/AAAA" id="bike_acq_date_id" type="date" required>

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