【发布时间】:2011-09-15 15:48:28
【问题描述】:
所以我已经被这个问题困扰了一天左右,但我仍然没有走得太远。我正在使用 django 内联表单集,这允许显示所有以前保存的表单,然后显示一个额外的空表单。我正在使用 javascript (jQuery) 隐藏所有表单,直到用户单击“编辑”或“添加新”按钮(p 标签样式看起来像一个标签),然后表单是 slideToggled 供用户查看然后编辑.
我遇到的问题是,根据 forms.py 中的正则表达式,表单无效。显然,表单没有保存,但 slideToggle 关闭了。如果您再次单击编辑按钮,则会在输入错误的字段上打开错误消息。
我想要的是,如果存在这些错误消息,我希望打开表单并显示需要更正的字段。这是提交表单之前的代码:
<div>
<p class="show_edit_ops button black small">Add New »</p>
<p class="show_edit_ops button black small" style="display:none;">Nevermind «</p>
<div class="formWrapperClass">
<div class="fieldWrapper">
<div class="form_errors"></div>
<p class="field_label"><label for="id_location_set-0-street">Street</label>:</p>
<p class="form_field"><input type="text" name="location_set-0-street" id="id_location_set-0-street" /></p>
<p class="field_help_text">Enter your residence number and street</p>
</div>
<div class="fieldWrapper">
<div class="form_errors"></div>
<p class="field_label"><label for="id_location_set-0-street2">Street2</label>:</p>
<p class="form_field"><input type="text" name="location_set-0-street2" id="id_location_set-0-street2" /></p>
<p class="field_help_text">Enter your apartment number or P.O. box</p>
</div>
<div class="fieldWrapper">
<div class="form_errors"></div>
<p class="field_label"><label for="id_location_set-0-city">City</label>:</p>
<p class="form_field"><input type="text" name="location_set-0-city" id="id_location_set-0-city" /></p>
<p class="field_help_text">Enter your city</p>
</div>
表格还在继续,但你明白了。这是提交后输入错误的 fieldWrapper div 之一:
<div class="fieldWrapper">
<div class="form_errors"><ul class="errorlist"><li>You must enter a street</li></ul></div>
<p class="field_label"><label for="id_location_set-0-street">Street</label>:</p>
<p class="form_field"><input type="text" name="location_set-0-street" id="id_location_set-0-street" /></p>
<p class="field_help_text">Enter your residence number and street</p>
</div>
我已经尝试了很多东西来测试是否存在错误列表,如果存在,我可以设置该表单的样式,使其在提交错误后打开。我已经非常广泛地尝试了 find 和 children,但可能是错误的,方式如下:
if($(".form_errors").find($(".errorlist")) == true){
document.write("error!");
}
但没有运气。如果我取出 == true 那么我会得到“错误!”在我网站的每个页面上。我很困惑,所以任何帮助都会很棒。我真正需要的是正确检查错误列表是否存在,我很确定在那之后我可以接受它。
谢谢!!
【问题讨论】:
标签: javascript jquery django validation django-forms