admin管理员组

文章数量:1287839

Hee guys. I have a form with a dropdown for all panies. Now i want to display all the people that work for that pany when the user changes the value in the pany dropdown box without hitting the submit button on the form. Anybody have a good example for that?

tks

Hee guys. I have a form with a dropdown for all panies. Now i want to display all the people that work for that pany when the user changes the value in the pany dropdown box without hitting the submit button on the form. Anybody have a good example for that?

tks

Share Improve this question edited Sep 20, 2009 at 22:44 Fragsworth 35.6k27 gold badges85 silver badges98 bronze badges asked Sep 20, 2009 at 22:41 Peter NewmanPeter Newman 1172 silver badges7 bronze badges 2
  • 1 This is more about ajax/javascript than django. I suggest you read a tutorial on ajax. – Fragsworth Commented Sep 20, 2009 at 22:44
  • @Fragsworth true, it's not necessarily about django. But it's also true that it doesn't require javascript or ajax. – TM. Commented Sep 21, 2009 at 2:20
Add a ment  | 

2 Answers 2

Reset to default 9

No Ajax necessarily needed, since you can just submit the form, and whether you're sing Django or other server-side choices make no difference, just use something like...:

<form name=paniesForm action="whateverurl" method=POST>
  <p>
  <select name=panySelect size=1 onChange="paniesForm.submit();">
    <option value="" SELECTED>Choose A Company
    <option value="1">One Company
    <option value="2">Another Company
    <option value="3">A Third Company
  </select>
  </p>
</form>

Alex's answer is a good route to go, but here's an alternative. Django and slugs go together rather well. And unobtrusive javascript with jquery is hip at the moment.

Instead of POSTing a value, you could simply navigate to a well-constructed URL. This has the added benefit of making the pages more SEO friendly, letting people bookmark the page, and also avoiding that silly error about POST'ed information when someone clicks the back button.

Note that in either bit of code, Alex's or mine, the navigation will break if javascript is disabled on the client browser. It'd be a good idea to provide some footer links to whatever this bo box does somewhere on the page (the bottom maybe).

(untested, might need some slight tweaks)

<!-- you'll need jquery to make this work -->
<script type="text/javascript">
     $(function() {
          // navigate to page on click event
          $('#nav_bo').bind('change', function() { goToPage(); } );
     });

     function goToPage() {
      var baseUrl = '/your/base/url/';
      window.location.href = baseUrl + $('nav_bo').val()
     }
</script>

...

<form>
     <select id="nav_bo">
          <option value="page-1-slug">Page 1</option>
          <option value="page-2-slug">Page 2</option>
     </select>
</form>

Edit -- By the way, I should have mentioned you could easily use the code above plugged into django's object_detail generic view.

本文标签: javascriptDjango Dropdown Auto submitStack Overflow