2017-03-09 20 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][]">