admin管理员组文章数量:1289679
I need to set a div to show or hide based on the parameter that passed in from django views.py, but I just couldn't get it to work. My code is like the following:
In the body:
{% if report_type == "detail" %}
visibility("detail");
{% endif %}
In javascript:
<script type="text/javascript">
function visibility(reportType) {
console.log(reportType);
if(reportType == "detail") {
$('#executive_summary').style.display = 'none';
}
}
</script>
However the above code just doesn't work. Can somebody give me some suggestions? Thanks.
I need to set a div to show or hide based on the parameter that passed in from django views.py, but I just couldn't get it to work. My code is like the following:
In the body:
{% if report_type == "detail" %}
visibility("detail");
{% endif %}
In javascript:
<script type="text/javascript">
function visibility(reportType) {
console.log(reportType);
if(reportType == "detail") {
$('#executive_summary').style.display = 'none';
}
}
</script>
However the above code just doesn't work. Can somebody give me some suggestions? Thanks.
Share Improve this question edited Apr 18, 2012 at 17:21 bonsaiviking 6,0051 gold badge23 silver badges37 bronze badges asked Apr 18, 2012 at 15:08 Shang WangShang Wang 25.6k21 gold badges77 silver badges95 bronze badges 2- Are you using jQuery or normal js? Two dont mix like your syntax does. – Henrik Andersson Commented Apr 18, 2012 at 15:11
-
Where is the
{% if...
block going? If you don't place it in a script element, and in one that exists after the page has finished loading (either by using jQuery's$(function onLoad(){})
magic or by putting a<script>visibility('detail')</script>
tag at the very end of your document) it will never go off. That is probably why your console line is never getting reached. Then you will need to fix your use of the jQuery api as everyone has mentioned. – quodlibetor Commented Apr 22, 2012 at 21:34
3 Answers
Reset to default 9By pure CSS:
{% if report_type != "detail" %}
<div style="display: none;">
{% else %}
<div style="display: block;">
{% endif %}
...
</div>
Or by using JavaScript (jQuery):
{% if report_type != "detail" %}
<script type="text/javascript">
$('#executive_summary').hide();
</script>
{% endif %}
By JavaScript functions:
<script type="text/javascript">
/* If wanted to run when page is loaded. */
$(function() {
// Handler for .ready() called.
visibility("detail");
});
function visibility(reportType) {
if(reportType == "detail") {
$('#executive_summary').show();
}
else {
$('#executive_summary').hide();
}
}
</script>
The problem is you are using the nonexistent style
attribute of the jQuery object returned by your '#executive_summary'
selector. jQuery objects don't have the same attributes and methods as DOM nodes. Try this instead:
<script type="text/javascript">
function visibility(reportType) {
console.log(reportType);
if(reportType == "detail") {
$('#executive_summary').hide();
}
}
</script>
This will do what you want, but if you absolutely, necessarily, must directly set the display attribute, you could replace that one line with:
$('#executive_summary').css('display', 'none');
But that is not as clean as just using .hide()
You need to put the function call in a javascript tag. Try this instead:
<script type="text/javascript">
{% if report_type == "detail" %}
$('#executive_summary').toggle();
{% endif %}
</script>
本文标签:
版权声明:本文标题:javascript - python django: How can I determine if a div should show or hide based on the parameter passed to template - Stack O 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741484891a2381354.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论