0

I've got a list of 10-20 objects on each page creating these forms:

<div id="routeTable">
  {% for route in route_list %}
  <div id="routeDone">
    <form class="doneForm" action="/route/complete/" method="post">
      <input type="hidden" name="route_id" value="{{ route.route_id }}" />
      <input type="hidden" name="next" value="{{ request.get_full_path }}" />
      <input type="submit" value="Done" class="doneButton" />
    </form>
  </div>
  {% endfor %}
</div>

And I'm trying to add some jquery to the page in order to intercept the usual form submit and instead do so using ajax as below. (I already have a view that returns an html chunk which will be swapped out for the above div#routeTable. The problem is line 4 "var route_ID...":

<script>
$(document).ready(function() {
  $(".doneForm").submit(function() {
    var route_id = $(this).attr('input[name=route_id]').val()

    $.ajax({
      type: "post",
      url: "/route/complete/",
      data: route_id,
      success: function(data) {
        $("#routeTable").html(data);
      }
    });
  return false;
  });
});
</script>

Unfortunately I'm having trouble passing the proper route_id into the js variable named route_id. I expect it will require use of the 'this' keyword, but I haven't been able to figure out exactly how.

Any suggestions on how to fix up my javascript would be greatly appreciated.

2 Answers 2

1

Try to change

var route_id = $(this).attr('input[name=route_id]').val()

to

var route_id = $(this).find('input[name=route_id]').val()

The reason being that input[name=route_id] is not an attribute, but a selector that represent a tag input and an attribute on that tag [name=route_id].

You could also do

var route_id = $('input[name=route_id]',this).val()
Sign up to request clarification or add additional context in comments.

Comments

0
var route_id = $(this).attr('input[name=route_id]').val()

Should be something like:

var route_id = $(this).find('input[name=route_id]').val()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.