带有输入元素的jQuery .each()

hlswsv35  于 2022-12-22  发布在  jQuery
关注(0)|答案(4)|浏览(170)
//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());
        });
    });
});

<div class="column0"><input type="text" value="-D"></div>
<div class="column1"><input type="text" value="D"></div>
<div class="column2"><input type="text" value="D"></div>
<div class="column3"><input type="number" value="0"></div>
<div class="column4"> <input type="number" value="0"></div>
<div class="column5"> <input type="number" value="0"></div>
<div class="column6"><input type="number" value="0"></div>
<div class="column7"><input type="number" value="0"></div>
<div class="column8"><input type="number" value="0"></div>
<div class="column9"> <input type="number" value="0"></div>
<div class="column10"> <input type="number" value=""></div>
<div id="tablet17row0" class="column11">11</div>
<div class="column0"><input type="text" value="-D"></div>
<div class="column1"><input type="text" value="D"></div>
<div class="column2"><input type="text" value="D"></div>
<div class="column3"><input type="number" value="0"></div>
<div class="column4"> <input type="number" value="0"></div>
<div class="column5"> <input type="number" value="0"></div>
<div class="column6"><input type="number" value="0"></div>
<div class="column7"><input type="number" value="0"></div>
<div class="column8"><input type="number" value="0"></div>
<div class="column9"> <input type="number" value="0"></div>
<div class="column10"> <input type="number" value=""></div>
<div id="tablet17row1" class="column11">21</div>

我有上面的jQuery .each(),它将附加的HTML输出到控制台。在本例中,我只想提取text或number类型的输入元素的val()。是否有某种方法可以只隔离输入元素,以便将它们的值输出到数组中?

rsl1atfo

rsl1atfo1#

提取编号:

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();
i5desfxk

i5desfxk2#

假设所有的输入元素都在一个表单中,u可以引用下面的代码。

// 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();
    });
iyfamqjs

iyfamqjs3#

$.each($('input[type=number]'),function(){
  alert($(this).val());
});

这将提醒input type number字段的值
http://jsfiddle.net/2dJAN/33/上有演示

watbbzwu

watbbzwu4#

您可以用途:

$(formId).serializeArray();

相关问题