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
3 Answers
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
版权声明:本文标题:javascript - jQuery loop through all text fields with class and get the value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741987771a2408785.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论