jquery - 检查模板中的表单字段是否为空

标签 jquery django django-templates

关于问题:- 我有一个名为主题的表单字段,它是 Manytomanyfield。现在,在模板中,我在字段集中的 div 中调用 {{form.topics}} 。我想检查 {{form.topics}} 是否为空或其长度为 <=1 在这种情况下我不想显示 {{form.topics}} 的字段集这是我的代码。我正在使用 jquery 解决这个问题。

 forms.py  
 # Showing only that field to keep code short 
    class VisitSetupForm(Form):
    topics = ModelMultipleChoiceField(
                    queryset=Topic.objects.filter(reporting=False),
                    widget=CheckboxSelectMultiple,
                    required=False
                )

    Views.py
    def setup(request):
        if request.user.is_superuser:
            form_class = AdminVisitSetupForm
            all_topics = True
        else:
            form_class = VisitSetupForm
            all_topics = False

        f = form_class(request, data=request.POST or None)
        if request.method == "POST":
            if f.is_valid():
                ......so on ....
                if request.user.is_superuser:
                    topics = cd['topics']
                else:
                    topics = set(list(interview.topics.all()) + list(cd['topics']))
            next_url = "/visit/confirmation/%s/%s/?next=%s" % (patient.user.id, interview.id, url)
            return HttpResponseRedirect(next_url)
    if not all_topics:

       user = get_user(request)
       # checking here if the topics exists for other user
       f.fields['topics'].queryset = user.organization.topics
       f.fields['interview'].queryset = user.organization.interviews

       data['form'] = f
     return render_to_response('visit/setup.html', data, context_instance=RequestContext(request))   

    .html
    # calling in html
    <fieldset class="step4">
            <legend>Step 4 - Topic selection</legend>
            <p>Check off any additional topics you want to add to the interview. If you want to
            remove a topic from an interview, uncheck it.</p>
            <div>{{ form.topics }}</div>
        </fieldset>
        <script>
            if($(".step4 input:checkbox").length <= 0)
            {
                $(".step4").hide();
            }
        </script>

{{form.topics}} 是复选框列表。我希望当没有复选框时({{form.topics 为空}})不显示字段集 这是通过 jquery 实现的。我想要类似 {{form.topics.empty}} 的东西不显示 步骤 4 字段集。有什么好的方法可以删除该 jquery。

提前致谢..

最佳答案

我建议你计算变量的长度

forms.topics

在您的 View 中,只需在模板中使用此变量即可

{% if not forms.topic or variable <= 1 %}
    <td>Whatever you want to display</td>
{% else %}
    <td> {{ forms.topic }} </td>
{% endif %}

此代码检查“forms.topic”中是否没有值或变量的长度(您在 View 中计算的)是否小于或等于 1。打印您要显示的文本。

关于jquery - 检查模板中的表单字段是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9209525/

相关文章:

javascript - 检查对象是否在 css border/css wrapper 内

javascript - 从客户端向 ASP.NET 中的一个 ListBox 控件添加/删除项目到另一个 ListBox 控件

javascript - NodeJS Express 无法接收 POST json

mysql - 我想知道 Django 查询集中的排名

django - 使用 Django ORM (CROSS JOIN) 计算组合

python - 渲染而不传递请求

javascript - 如何使用<td>值进行jquery计算代替&lt;input type ="number">

java - 用于 post 请求的 java applet 和 django 服务器之间的 http 连接

python - 根据用户来自哪个页面来区分用户。 Django

javascript - 在实际元素环境 (Django) 中鼠标悬停时后台移动的工作 Jsfiddle 示例不起作用?