使用多个输入字段填充数组

时间:2012-09-26 20:03:21

标签: javascript jquery arrays

我想使用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 ??????????????

});

5 个答案:

答案 0 :(得分:7)

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

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

如果需要,可以使用jQuery获取.value

return $(this).val();

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

答案 1 :(得分:0)

$('.seourl').each(function(ele){
    arr_str.push($(ele).val());
});

答案 2 :(得分:0)

$("#getFriendlyUrl").click(function() {
    var arr_str = new Array();

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

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

});

答案 3 :(得分: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 w / 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/

答案 4 :(得分:0)

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

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

    });
    alert(arr_str);
});
相关问题