2012-09-26 151 views
0

我想使用jQuery来填充类名为seourl“从输入字段值的数组...填入阵列多个输入字段

<input id="title" class="seourl" name="title" type="text" value="???"> 
<input id="subtitle" class="seourl" name="title" type="text" value="???"> 
<input id="subtitle2" class="seourl" name="title" type="text" value="???"> 

<a id="getFriendlyUrl" href="">get url friendly</a> 

我如何填充的输入字段数组班'seourl'?

$("#getFriendlyUrl").click(function() { 

    var arr_str = new Array(); 

    ?????? POPULATE ARRAY with input fields of class 'seourl', how ?????????????? 

}); 

回答

7
$("#getFriendlyUrl").click(function() { 

    var arr_str = $('.seourl').map(function() { 
             return this.value; 
            }).toArray(); 
}); 

您可以使用jQuery来获取.value如果你想。

return $(this).val(); 

无论哪种方式,你最终会得到一个值的数组。

+1

'.toArray()'? '.map'的返回值不会是一个数组吗? –

+0

我更喜欢你的答案。 :) – Shmiddty

+1

@FabrícioMatté:不,这可能是原生的'Array.prototype.map'或'$ .map'。这个返回一个jQuery对象。 :-) –

0
$('.seourl').each(function(ele){ 
    arr_str.push($(ele).val()); 
}); 
0
$("#getFriendlyUrl").click(function() { 
    var arr_str = new Array(); 

    $('.seourl').each(function() { 

     arr_str.push($(this).val()); 
    })' 

}); 
0

HTML:

<input id="title" class="seourl" name="title" type="text" value="???"> 
<input id="subtitle" class="seourl" name="title" type="text" value="???"> 
<input id="subtitle2" class="seourl" name="title" type="text" value="???"> 

<a id="getFriendlyUrl" href="">get url friendly</a>​ 

JS瓦特/ jQuery的:

$("#getFriendlyUrl").click(function() { 

    var arr_str = new Array(); 

    $(".seourl").each(function(index, el) { 
    arr_str[index] = $(el).val(); 
    }); 
    alert(arr_str[0] + arr_str[1] + arr_str[2]); 

});​ 

的jsfiddle:http://jsfiddle.net/Mutmatt/NmS7Y/5/

0
$("#getFriendlyUrl").click(function() { 

    var arr_str = new Array(); 
    $('.seourl').each(function() { 
     arr_str.push($(this).attr('value')); 

    }); 
    alert(arr_str); 
});