2012-01-18 37 views
0

我需要隱藏內置表的零件/是動態生成的,如下圖所示的表格顯示部分。如果未勾選「複選框」,我想用class =「newtable」顯示或隱藏表格。但是,我只想隱藏與循環迭代對應的實例。因此,如果複選框被選中,用戶看到的額外信息,並具有以填補它。如果沒有,他們沒有看到它,並沒有採取進一步的行動。該邏輯將應用於表格的每一行。我可以看到如何使用JavaScript來隱藏「newtable的」所有情況下,但我只是想隱藏那些符合「複選框」被檢查。任何幫助是極大的讚賞。Django的新手 - 需要幫助隱藏了動態

順便說一句,我因爲我的變量來自不同的模型來建立這個「形式」很長的路要走。無法看到使用django表單,模型表單或formset來完成的任何方式。謝謝!

<table> 
<tr> 
{% for x,y,z in stuff%} 
    <td>{{ x.foo }}</td> 
    <td>{% for item in y %} 
     <input type="checkbox" name="stuff.{{ item.id }}" class="item" value="True" checked/> {{ item }}<br />   
    {% endfor %} 
    </td> 
    </tr> 
    <tr><td align=center colspan="5"> 
    <table class="newtable" border=1> 
    <tr><td> 
    <input type="radio" name="pref_id{{ z.id }}" value="1" checked> blah<br> 

{% endfor %} 
</td> 
</tr> 
</table> 
</tr> 
    {% endfor %} 

</table> 

回答

1

您可以使用jQuery輕鬆隱藏table .newtable下的所有checked複選框。

<!DOCTYPE html> 
<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> 
    </script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $(".newtable input[type=checkbox]:checked").hide(); 
     }); 
    </script> 
</head> 
<body> 
    <table class="newtable"> 
     <tr><td><input type="checkbox" checked/></tr> 
     <tr><td><input type="checkbox"></tr> 
    </table> 
</body> 

如果你願意將不得不整個行隱藏,你可以使用

$(".newtable input[type=checkbox]:checked").parent().parent().hide() 

另外,您可以生成表單時隱藏起來,假設你設置一些複選框託運取決於項目。檢查

{% if item.checked %} 
    <input type="checkbox" checked style="display:none" ..../>  
{% else %} 
    <input type="checkbox" ..../>  
{% endif %} 
+0

感謝您的回答。我結束了一些非常相似的事情。唯一的區別是,我需要一個唯一的ID動態添加到我想躲的元素,然後使用jQuery來隱藏它。非常感謝您的幫助! – mb52089 2012-01-18 21:16:30