2013-05-17 91 views
17
//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到控制檯。在這種情況下,我想提取只有輸入元素的val(),無論是文本類型還是類型編號。有什麼辦法來隔離輸入元素,以便我們可以將它們的值輸出到數組中嗎?jQuery .each()與輸入元素

在此先感謝...

回答

32

要提取號碼:

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

假設所有的輸入元素都在表單裏面,你可以參考下面的代碼。

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