admin管理员组

文章数量:1315831

I am trying to loop through all input=text fields that have a specific class and extract the value of the input fields using jQuery. I can't seam to get the value though as no matter what, it gives me "(an empty string)" in the console.

How can I get the value? My code is:

$.each($('.datepicker'), function() {
    console.log(this);
    console.log($(this).val());
});

The output of this is:

<input id="createdDate" class="datepicker hasDatepicker" type="text" onchange="setDueDate();" value="2013-06-19" size="8" name="invoice[createdDate]">
(an empty string)
<input id="dueDate" class="datepicker hasDatepicker" type="text" value="2013-06-19" size="8" name="invoice[dueDate]">
(an empty string)

Edit: It seams the issue was part cached JS (even though the file was showing correctly in the source, it was executing the older JS...very odd), and part the jQuery datepicker screwing things up. I fixed it like so:

$(document).ready(function() {
    $('.datepicker').datepicker();

    $.each($('.datepicker'), function() {
        date = $(this).val();

        $(this).datepicker('option', 'dateFormat', 'yy-mm-dd');
        $(this).datepicker('setDate', date);
    });
});

I am trying to loop through all input=text fields that have a specific class and extract the value of the input fields using jQuery. I can't seam to get the value though as no matter what, it gives me "(an empty string)" in the console.

How can I get the value? My code is:

$.each($('.datepicker'), function() {
    console.log(this);
    console.log($(this).val());
});

The output of this is:

<input id="createdDate" class="datepicker hasDatepicker" type="text" onchange="setDueDate();" value="2013-06-19" size="8" name="invoice[createdDate]">
(an empty string)
<input id="dueDate" class="datepicker hasDatepicker" type="text" value="2013-06-19" size="8" name="invoice[dueDate]">
(an empty string)

Edit: It seams the issue was part cached JS (even though the file was showing correctly in the source, it was executing the older JS...very odd), and part the jQuery datepicker screwing things up. I fixed it like so:

$(document).ready(function() {
    $('.datepicker').datepicker();

    $.each($('.datepicker'), function() {
        date = $(this).val();

        $(this).datepicker('option', 'dateFormat', 'yy-mm-dd');
        $(this).datepicker('setDate', date);
    });
});
Share Improve this question edited Jun 20, 2013 at 9:46 shiznatix asked Jun 20, 2013 at 8:48 shiznatixshiznatix 1,1071 gold badge20 silver badges38 bronze badges 1
  • just set up a fiddle with your code and it is working as expected - jsfiddle/YR892 – user700284 Commented Jun 20, 2013 at 8:52
Add a ment  | 

3 Answers 3

Reset to default 6
$('.datepicker').each(function(index, item) {
    console.log($(item).val());
});

should work

$('.datepicker').each(function(){
   console.log(this.value);
});
$(.datepicker).each(function(index, element)){
    console.log($(element).val());
});

本文标签: javascriptjQuery loop through all text fields with class and get the valueStack Overflow