2014-09-05 24 views
0

我想設置一個函數內的函數傳遞onClick時的JavaScript值,然後我想我的函數採取點擊目標的ID保存到一個var,所以我可以基於其返回的javascript var id在該對象上調用.show。Django + Jquery + Tabs,標籤不會顯示在點擊

這是我如何輸出列表鏈接:

{%for stat in ip_new_list %}       
    {% if 'Newip' in stat%} 
     <li class="active"><a href onClick="showTable();" id='{{stat.1}}'>{{stat.1}} </a></li> 
    {%endif%} 
{%endfor%}        
</ul> 

我隱藏所有與類名使用下面的代碼標籤:

$(document).ready(function(){ 
     $('#loading').hide(); 
     $('#helppage').hide(); 
     $('.iptable').hide(); 
    }); 

這是我的代碼爲了顯示帶有被點擊鏈接的ID的div標籤,我已經驗證了div標籤id匹配鏈接id,該鏈接id是那種格式的'100.000.000.000'ip地址。

function showTable(clicked){ 
    console.log("ShowTableClicked"); 
    console.log(clicked.id); 
    var clickedID = "#" + clicked.id; 
    $(clickedID).show(); 
    console.log(clickedID); 
} 

這裏是我的Django/HTML代碼:

<ul> 
{%for stat in ip_new_list %}       
    {% if 'Newip' in stat%} 
     <li class="active" id={{stat.1}}><a href='#' onClick="showTable(this);" id={{stat.1}}>{{stat.1}}</a></li> 
    {%endif%} 
{%endfor%}        
</ul> 


{%for stat in ip_new_list %} 
     {% if 'Break' in stat%} 
       </div> <div id="{{stat.1}}" class='iptable'> 
     {%endif%} 

     {% if 'First' in stat%} 
       <div id="{{stat.1}}" class='iptable'> 
     {%endif%} 


      {{stat.0}} | {{stat.1}} | {{stat.2}} <br> 

{%endfor%} 

這裏是控制檯輸出:

"ShowTableClicked" ipreport:56 
"000.000.000.000" ipreport:57 
"#000.000.000.000" 

但div標籤仍然沒有出現我用調試檢查員驗證div標籤是否有正確的ID。請讓我知道你是否可以找出原因。

+0

'$( '#' + clickedID).show();' – bencripps 2014-09-06 02:15:48

回答

0

這裏是正確的工作答案。我不得不添加鏈接到鏈接的id,因爲它沒有找到div標籤,因爲他們都有這個id,它正在改變樣式或兩者,所以爲了解決這個問題,我只是附加鏈接並刪除取代這個效果很好。現在,它的時間,風格和使用淡出而不是顯示。

感謝您的幫助。

{%for stat in ip_new_list %}       
    {% if 'Newip' in stat%} 
     <a href='#' onClick="showTable(this);" id="{{stat.1}}link">{{stat.1}}</a> 
    {%endif%} 
{%endfor%}        



<div class="table-responsive"> 
{%for stat in ip_new_list %} 
     {% if 'Break' in stat%} 
       </tbody></table></div> <div id="{{stat.1}}" class='iptable'> <table class="table" id='table-data'> <tbody> 
     {%endif%} 
     {% if 'First' in stat%} 
       <div id="{{stat.1}}" class='iptable'><table class="table" id='table-data'><tbody> 
     {%endif%}  

     {% if 'Break' in stat%} 
     {%else%} 
      {% if 'First' in stat%} 
       {%else%} 
      <tr> 
       <td>{{stat.0}}</td> 
       <td>{{stat.1}}</td> 
       <td>{{stat.2}}</td>   
      </tr> 
     {%endif%} 
     {%endif%} 

{%endfor%} 

</div> 

在頁面的底部添加了腳本並將其更改爲此。

<script> 


function showTable(clicked){ 
    $(".iptable").hide(); 
    var id = clicked.id 
    id = id.replace('link', ''); 
    console.log(id); 
    document.getElementById(id).style.display = "block"; 
} 

</script> 
0

您的問題是在jQuery選擇線 - $("#" + clickedID).show();

選擇元素jQuery中由ID(和CSS,順便說一句)與#開始。

此處瞭解詳情: http://api.jquery.com/id-selector/

+0

這不是因爲我已經做了它的修改版本,但它不工作的解決方案。 – 2014-09-08 20:11:39