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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16601326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:27:25  来源:igfitidea点击:

jQuery .each() with input elements

jqueryinputeach

提问by Eae

//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>

I have the above jQuery .each() that outputs the attached HTML to the console. In this case I want to extract the val() of only the input elements either of type text or type number. Is there some way to isolate just the input elements so I can get their values out into an array?

我有上面的 jQuery .each() 将附加的 HTML 输出到控制台。在这种情况下,我只想提取文本类型或数字类型的输入元素的 val()。有没有办法只隔离输入元素,以便我可以将它们的值放入数组中?

Thanks in advance...

提前致谢...

回答by Karl-André Gagnon

To extract number :

提取数字:

var arrNumber = new Array();
$('input[type=number]').each(function(){
    arrNumber.push($(this).val());
})

To extract text:

提取文本:

var arrText= new Array();
$('input[type=text]').each(function(){
    arrText.push($(this).val());
})


Edit : .mapimplementation

编辑:.map实施

var arrText= $('input[type=text]').map(function(){
    return this.value;
}).get();

回答by SivaRajini

Assume if all the input elements are inside a form u can refer the below code.

假设所有输入元素都在表单内,您可以参考以下代码。

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

回答by vinothini

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

This will alert the value of input type numberfields

这将提醒input type number字段的值

Demo is present at http://jsfiddle.net/2dJAN/33/

演示存在于http://jsfiddle.net/2dJAN/33/

回答by Lê V?n Chi?n

You can use:

您可以使用:

$(formId).serializeArray();