简体   繁体   中英

I am getting undefined when trying to return the date value from an input field

JAVASCRIPT

I am just trying to returen the value of the date selected but it's coming back undefined.

var getDate = function() {              
    var bdinput = $('#bd.bdinput').val();
    var _this = bdinput;                    
    console.log(_this);

};

$("button.bdsubmit").click(function() {                         
    console.log(getDate());
})

HTML

<form id="bd">                  
<input name="BirthDate" class="bdinput" type="date" />
<button class="bdsubmit"  type="submit">Submit Date</button>

You need .val() to get value of input not .value

var bdinput = $('#bd .bdinput').val();
                 // ^ also need space here

$('#bd .bdinput') will select element with class bdinput inside element with id bd .

$('#bd.bdinput') will select a element with id 'bd' and class bdinput (both id and class in one element)


If you want to do it with .value

 var bdinput = $('#bd .bdinput')[0].value; 

Or

 var bdinput = $('#bd .bdinput').get(0).value; 

It was a simple mistake.

var bdinput = $('#bd.bdinput').value;

should be:

var bdinput = $('#bd. bdinput').val();

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