Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:循环类并推送输入_Javascript_Jquery_Html - Fatal编程技术网

Javascript:循环类并推送输入

Javascript:循环类并推送输入,javascript,jquery,html,Javascript,Jquery,Html,将某些输入值推送到数组时遇到问题。我有一个div和一个可以克隆的类optiondiv。此div有一些表行和一些输入字段,如下所示: <div class="optiondiv"> <table class="tableOptions"> <tr> <td> <input type="text" class="title"> </

将某些输入值推送到
数组时遇到问题。我有一个
div
和一个可以克隆的类
optiondiv
。此div有一些表行和一些输入字段,如下所示:

<div class="optiondiv">
    <table class="tableOptions">
        <tr>
            <td>
                <input type="text" class="title">
            </td>

            <td>
                <input type="text" class="position">
            </td>
            <td>
                <select class="input">
                    <option value="1">Drop-down</option>
                    <option value="2">Radio Buttons</option>
                    <option value="3">Checkbox</option>
                    <option value="4">Multiple Select</option>
                </select>
            </td>
            <td>
                <input type="checkbox" class="required"> Required?
            </td>
        </tr>
        </tbody>
    </table>
</div>
这是我正在使用的de代码:

$('.generateString').click(function (e)
    {
        e.preventDefault();
        $('.optiondiv').each(function(){
            var arrText = new Array();

            $('input[type=text]').each(function ()
            {
               if($(this).val() != ''){
                   // arrText.push('nietleeg');
                   arrText.push($(this).val());
                }
            })
            console.log(arrText);
        })

    });

快乐编码

首先,而不是

$('input[type=text]').each(function ()
您需要使用它来获取每个.optiondiv中的输入元素

$(this).find('input[type=text]').each(function ()
根据您的需求,只需在外部定义数组变量,并在每个循环内部创建关联数组,如下所示

$('.generateString').click(function(e) {
    e.preventDefault();
    var arrText = new Array();
    $('.optiondiv').each(function(i) {
        if (typeof arrText[i] == "undefined")
            arrText[i] = new Array();

        $(this).find('input[type=text]').each(function() {
            if ($(this).val() != '') {
                // arrText.push('nietleeg');
                arrText[i].push($(this).val());
            }
        })
        console.log(arrText[i]);
    })

    console.log(arrText);

});

您的html中没有“generateString”类。我没有粘贴整个html,因为这可能很难阅读。try:`arrText.push([$(this.val())`你能贴一把小提琴吗something@KoenvandeSande在
[Doors,1,Windows,5]
中,什么是
Doors
Windows
!谢谢但是有没有一种方法可以向数组中添加索引,这样我也可以循环遍历这些数组?很抱歉没有得到你要的,你能详细说明一下吗?我现在有这些值,但它们没有索引对吗?意味着你需要键,值对?是的,我想是这样的。
$('.generateString').click(function(e) {
    e.preventDefault();
    var arrText = new Array();
    $('.optiondiv').each(function(i) {
        if (typeof arrText[i] == "undefined")
            arrText[i] = new Array();

        $(this).find('input[type=text]').each(function() {
            if ($(this).val() != '') {
                // arrText.push('nietleeg');
                arrText[i].push($(this).val());
            }
        })
        console.log(arrText[i]);
    })

    console.log(arrText);

});