2017-03-09 21 views
0

的指数I有一个小的这样的代码:得到在HTML(jQuery的)阵列状名称属性

<div class="question_wrapper"> 
    <input type="text" class="textinput_questions new_question" name="new_question[1][]"> 
    <input type="checkbox" class="radioinput" name="new_questionActive[]"> 
</div> 

我生成在一个循环中,工作正常这些输出。 name="new_question[1][]"中的硬编码1将上升到3,所以我在这种模式下有3个索引。

现在,我想运行通过jQuery的AJAX请求,它看起来像这样:

$('.new_question').each(function() { 
    // for each added question, we call a script which then inserts the new questions 
     console.log(this); 
    }); 

到目前为止是这种情况,为正确的元素在我的控制台打印。但是,因为我需要索引的第一级,所以我需要以某种方式得到该值,因为这将是一个将在稍后传递给上述ajax函数的参数表。

要再次澄清:

我想通过所有问题进行迭代。对于每个问题,我使用这些特定值创建一个ajax-Request。我需要第一个数组索引(在上面的代码中写成1。如果我想要3个问题,所有索引1和索引2都是索引2,那么我最终应该运行5次循环,定义在(假想的)可变categoryID 1(前3次运行)和2个在未来2点运行。

编辑

因为它似乎是有点不清楚,在1句的基本的东西:我需要从这样的模式得到数值(在我的案例中为1):

<input type="text" class="textinput_questions new_question" name="new_question[1][]"> 

通过循环遍历each -loop。

+0

这是一个有点不清楚 - 你可以尝试以不同的方式提问?有几种方法可以解释这个 –

+0

当然,我添加了一个编辑。也许这有助于澄清我的需求 – DasSaffe

+0

您的意思是输入的价值?像'$('input [name =“new_question [1] []”])。val()' –

回答

0

OK,我们可以做的是循环遍历每个元素并得到name属性值,然后删除一切,但数字

$('.new_question').each(function() { 
 
    console.log($(this).prop('name').match(/\d+/).toString()); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" class="textinput_questions new_question" name="new_question[1][]"> 
 
<input type="text" class="textinput_questions new_question" name="new_question[2][]"> 
 
<input type="text" class="textinput_questions new_question" name="new_question[3][]">