django jquery toggle in form from model

天涯浪子 提交于 2019-12-11 07:34:29

问题


I was wondering if I could get some guidance using jquery in django.

I have a form from model in which I would like to display certain form fields based on a check box being checked or unchecked. Could also be done using a button. I am already using Jquery datepicker and timepicker in the form which works fine. Jquery toggle is here http://api.jquery.com/toggle/. I can get it work elsewhere, just not inside the form.

Here is a some template code where 'e' is the form from model being passed in, the datepicker code included as a quide for how toggle should work, and toggle code using a button to toggle. Thanks in advance.

~ Rich

{% block head %}
<link type="text/css" href="/media/jquery-ui-1.8.7.custom/css/ui-lightness/jquery-ui-1.8.7.custom.css" rel="stylesheet" />   
    <script type="text/javascript" src="/media/jquery-ui-1.8.7.custom/js/jquery-1.4.4.min.js"></script>
    <script src="http://code.jquery.com/jquery-1.5.js"></script>
    <link type="text/css" href="/media/jquery-ui-1.8.7.custom/jquery.timepicker-1.1.0/jquery.timepicker-1.1.0.css" rel="stylesheet" />   
    <script type="text/javascript" src="/media/jquery-ui-1.8.7.custom/jquery.timepicker-1.1.0/jquery.timepicker-1.1.0.js"></script>
    <script type="text/javascript" src="/media/jquery-ui-1.8.7.custom/js/jquery-ui-1.8.7.custom.min.js"></script>
{% endblock %}

{% block content %}
<script type="text/javascript">
<!--jquery datepicker-->
$(function() {
    $("#id_start_date, #id_end_date").datepicker({ showOn: "both",
        buttonImage: "/media/calendar.gif",
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yy-mm-dd' });
 });
</script>
<script>
$("#id_allday").click(function () {
$('#toggle').toggle("slow");
});   
</script>

<form action="" method="POST">
<fieldset style="width:700px; margin-left: auto; margin-right: auto; border: 1px solid #ff9900">
    <legend style="color: #ff9900; font-weight: bold">New</legend>
<table >
    <tbody>
    <tr><td style="vertical-align: top;">Date: {{e.start_date.errors}} {{e.start_date}}</td>
    <td style="vertical-align: top;">End Date {{e.end_date.errors}} {{e.end_date}}</td>
    </tr>
    <tr><td style="vertical-align: top;">Time: All Day {{e.allday.errors}} {{e.allday}}
<button>Add times</button></td>
    <div id='toggle'>
   <td style="vertical-align: top;">Start Time: {{e.start_time.errors}} {{e.start_time}}<br></td></tr>
    <td style="vertical-align: top;">End Time: {{e.end_time.errors}} {{e.end_time}}</td> </tr>
    </div>
    </tbody>
    </table>
<input value="Submit" type="submit"></fieldset></form>

</div>
{% endblock %}

回答1:


Try putting the jQuery event code inside an onload function, otherwise the form elements won't exist yet in the dom and there is nothing for jQuery to query yet.

$(document).ready(function() {
    $("#id_allday").click(function () {
        $('#toggle').toggle("slow");
    })
}

Also, you may want to bind to change instead of click incase someone toggles the input with the spacebar instead of the mouse button.



来源:https://stackoverflow.com/questions/5249732/django-jquery-toggle-in-form-from-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!