2015-12-21 12 views
0

出於某種原因,以下規格jQuery的每個函數:不能正常獲取數據如預期

模板標籤的Django我不能獲得第二TD text/html的。

{% block content %} 
{% if host.host_scripts %} 
<table class="table"> 
    <button class="btn btn-info add" id="{{forloop.counter}}">Add to session</button> 
    {% for script in host.host_scripts %} 
    <tr> 
     <td id="script-name-{{forloop.counter}}">{{script.scriptName}}</td> 
     <td id="script-output-{{forloop.counter}}">{{script.scriptOutput|linebreaks}}</td> 
    </tr> 
    {% endfor %} 
</table> 
{% endif %} 
<script> 
$(".add").click(function(e) { 
    var id = $(this).attr('id'); 
    scriptList = [] 
    e.preventDefault(); 
    $("#myTable-"+id+" tr ").each(function() { 
     var name = $(this).find('#script-name-'+id).html(); 
     var output = $(this).find('#script-output-'+id).html(); 
     var myList = [name, output]; 
     console.log('Name -> ' + name); 
     console.log('Output -> ' + output); 
     scriptList.push(myList); 
    }); 
}); 
</script> 
{% endblock %} 

我可以得到名稱變量就好了。出於某種原因,我無法獲取scriptOutput變量。

我認爲這是由於|linebreaks,但刪除它沒有影響行爲。

期望的行爲:

每表中的TR,拿到TD的內容,並添加到列表中。

當前的行爲:

只拿到的名稱變量,跳過由於某些原因scriptOutput變量。

我設法得到'一些'輸出,現在我得到相同的值爲每個scriptName輸出返回。我很困惑,因爲我看不到身份證有什麼問題。

{% block content %} 
{% if host.host_scripts %} 
<table class="table myTable"> 
    <button class="btn btn-info add" id="{{forloop.counter}}">Add to session</button> 
    {% for script in host.host_scripts %} 
    <tr> 
     <td id="script-name-{{forloop.counter}}">{{script.scriptName}}</td> 
     <td id="script-output-{{forloop.counter}}">{{script.scriptOutput|linebreaks}}</td> 
    </tr> 
    {% endfor %} 
</table> 
{% endif %} 
<script> 
var scriptList = [] 

$("#myTable-"+id+" tr").each(function() { 
    var name = $(this).find('td:first').html(); 
    var output = $('#script_output-'+id).html(); 
    var script = [name, output]; 
    scriptList.push(script); 
    console.log('Text: ' + output); 
}); 
console.log(scriptList); 
</script> 

我知道我應該擴大我的ID,以各種各樣的東西:

<td id="script-output-{{forloop.parentloop.counter}}-{{forloop.counter}}">{{script.scriptOutput|linebreaks}}</td> 

但我似乎無法弄清楚,我到底做錯了什麼?

+1

我懷疑你會得到要麼,你就錯過了'-'腳本name'之間'/'腳本output'和'id' – jcaron

+0

錯字:你的'id's是'script-output-N',但是你正在查詢'#script-outputN'(沒有連字符)。 –

+0

此外,您應該使用任何使用這些'{%...%}'和{{...}}替換的語言/框架來標記問題,或者提供HTML/JS,因爲它看起來像* *替換後**。 – jcaron

回答

-1

試試這個:

<script> 
$(".add").click(function(e) { 
    var id = $(this).attr('id'); 
    scriptList = [] 
    e.preventDefault(); 
    $("#myTable-"+id+" tr ").each(function(index, el) { 
     var name = $(el).find('#script-name-'+id).html(); 
     var output = $(el).find('#script-output-'+id).html(); 
     var myList = [name, output]; 
     console.log('Name -> ' + name); 
     console.log('Output -> ' + output); 
     scriptList.push(myList); 
    }); 
}); 
</script> 
+1

_「遍歷元素不會改變'this'的值」_ - [是的,確實](http://api.jquery.com/each/):_「回調在當前的上下文中被觸發DOM元素,所以關鍵字'this'指的是元素。「_ –

+0

你鏈接到的不是'each'方法。它在該頁面上表示如下:_「$ .each()函數與$(selector).each()」_ –

+0

不一樣感謝您的支持。 'this'確實指的是jQuery Docs中指出的回調函數的值參數。 https://api.jquery.com/each/ – JensDebergh