带有输入元素的jQuery .each()
//save tablet jQuery("#savetablet"+jTablets[i].idtablets).on('click', function() { alert("alertsepy2..."); console.log(jTablets[i].idtablets); jQuery("#tablet"+jTablets[i].idtablets+" .detailsrow").each(function( index ) { $(this).each(function( index2 ) { console.log($(this).html()); }); }); }); 11 21
我有上面的jQuery .each(),它将附加的HTML输出到控制台。 在这种情况下,我想只提取类型为text或type number的输入元素的val()。 有没有办法隔离输入元素,以便我可以将它们的值输出到数组中?
提前致谢…
提取数量:
var arrNumber = new Array(); $('input[type=number]').each(function(){ arrNumber.push($(this).val()); })
提取文字:
var arrText= new Array(); $('input[type=text]').each(function(){ arrText.push($(this).val()); })
编辑: .map
实现
var arrText= $('input[type=text]').map(function(){ return this.value; }).get();
假设所有输入元素都在一个表单内,你可以参考下面的代码。
// get all the inputs into an array. var $inputs = $('#myForm :input'); // not sure if you wanted this, but I thought I'd add it. // get an associative array of just the values. var values = {}; $inputs.each(function() { values[this.name] = $(this).val(); });
$.each($('input[type=number]'),function(){ alert($(this).val()); });
这将提醒input type number
字段的值
演示出现在http://jsfiddle.net/2dJAN/33/