0

模板是

<button type="submit" id="add" class="button_style" onclick="addreporter();" value="Add New Authorised Reporter">Add New Authorised Reporter</button>
<div id="authorisedreporter" style="display:none">
  <form method="post" action="{% url incident.views.about_me %}">{% csrf_token %}
    <table width="100%">
        <tr>
            <td style="width:100px;">First name:</td>
            <td>{{registerform.first_name}}{{registerform.first_name.errors}}</td>
        </tr>
        <tr>
            <td>Last name:</td>
            <td>{{registerform.last_name}}{{registerform.last_name.errors}}</td>
        </tr>
        <tr>
            <td>Daytime phone:</td>
            <td>{{createprofile.phone_daytime}}{{createprofile.phone_daytime.errors}}</td>
        </tr>
        <tr>
            <td>Mobile phone:</td>
            <td>{{createprofile.phone_mobile}}{{createprofile.phone_mobile.errors}}</td>
        </tr>
        <tr>
            <td>Email:</td>
            <td>{{registerform.email}}{{registerform.email.errors}}</td>
        </tr>
        <tr>
            <td>User name</td>
            <td>{{registerform.username}}{{registerform.username.errors}}</td>
        </tr>
        <tr>
            <td>Password</td>
            <td>{{registerform.password}}{{registerform.password.errors}}</td>
        </tr>
        <tr>
            <td colspan=2 "">
                <input type="checkbox" name="is_qualified_firstaiders" style="margin: 0;vertical-align:middle" />Qualified First Aiders</td>
        </tr>
    </table>
     </form>
    </div>

js:

function addreporter(){
        $("#authorisedreporter").toggle();
        if ($("#authorisedreporter").is(":visible")) {   
            $("#authorisedreporter").show();          
            $("#add").hide();
        }
    }

使用jquery切换,点击打开 <button>Add new Authorized Reporter</button>,一切都很好,除了如果表单中有任何错误,表单将处于隐藏模式,如果我想再次查看错误,我需要点击 <button>Add new Authorized Reporter</button>,它是一个小逻辑来设置错误是否在表单中,表单不应该隐藏。我想的是,如果我验证display:nonein <div> 将起作用,但不知道如何在模板中验证它。

谢谢

4

1 回答 1

0

你可以做:

<div id="authorisedreporter" {% if not registerform.errors %}style="display:none"{% endif %}>

此外,您可能需要addreporter()相应地进行修改。

于 2013-05-23T15:05:23.163 回答