jquery dynamic input field on select option onchange
I have a select option box with the value from database. If i select an option the other three text field will be loaded with data from database based on select option id or primary key value. how do i do that in django , jquery. 开发者_高级运维Thank you so much
In the spirit of teaching a man to fish, rather than just giving him some to eat...
- You need use to bind a callback function to the click or change event of the select input.
- This callback function should make
jQuery.ajax()
GET call to a view in your Django app that specifically sends back the data you want to add to the text fields (I'd recommend it comes back as JSON rather than HTML or plain text, as it'll be easier to process on the client/receiving end) - The callback function for the jQuery.ajax() call should then take the data that's been sent back and put the relevant sections of it into the relevant textfields (hence why a JSON structure will be really helpful for you)
If you need more cues than this, let us know, but reading the docs and/or Googling your way to how to do this will teach you more than a code-dump will.
Create a Django view which takes the select option id or primary key as a parameter. Have the view return the values you'll need in the other three text fields.
On the page with the form you'll need to have a bit of jQuery that, e.g. upon change of the selection field, calls the Django view and uses the result to fill the text fields.
How you'll format the result of the view is up to you. You could make it as simple as a comma separated list, you could also probably do something with JSON or get even fancier.
精彩评论