开发者

Django javascript problem when using forms

So I've been stuck on this problem for a day or so now and I'm still not getting very far. I'm using a django inline form-set, this allows for all previous saved forms to be displayed and then one extra empty form. I'm using javascript (jQuery) to hide all the forms until a user clicks an "edit" or "add new" button (p tag styled to look like an a tag) then the form is slideToggled for the user to see then edit.

The problem I'm running into is when the form isn't valid according to my regex in the forms.py. The form doesn't save, obviously, but the slideToggle closes. If you click the edit button again it opens with error messages over the fields that had incorrect input.

What I'm wanting is that if those error messages are present, I want the form to be open and showing which fields need to be corrected. Here's the code before submitting a form:

<div>
  <p class="show_edit_ops button black small">Add New &raquo</p>
  <p class="show_edit_ops button black small" style="display:none;">Nevermind &laquo</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>

And the form goes on, but you get the idea. And this is one of the fieldWrapper div's after submission with an incorrect input:

<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>

I've tried many things to test to see if errorlist exists, that way if it does I can style that form to be open after a bad submit. I've tried find and children pretty extensively, but possibly incorrectly, in ways like this:

if($(".form_errors").find($(".errorlist")) == true){
    document.write("error!");
    }

But with no luck. If I take out the == true then I get 'error!' on every page of my site. I'm baffled, so any help would be 开发者_StackOverflow中文版great. All I really need is a correct check to see if errorlist exists, I'm pretty sure I can take it after that.

Thanks!!


Try this:

if($(".form_errors").find($(".errorlist")).length){
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜