如何将AJAX用于相关的下拉列表?

时间:2019-01-25 09:54:49

标签: ajax django-models html-select

我希望我的“位置”下拉列表依赖于我的“客户”下拉列表。我的模特:

class Client(models.Model):
  name = models.CharField(max_length=50)

class Location(models.Model):
  name = models.CharField(max_length=50)

此后的下一步是什么?

我应该把FK放在哪里吗?

什么是AJAX代码?

1 个答案:

答案 0 :(得分:0)

如果您要显示两个下拉菜单,第一个带有<ul style="list-style-type: none;margin: 0;padding: 0; color:color:#ff00ff; background-color:darkblue"> <li style=display:inline;> <a href="" style="color:white"> Home </li> <li style=display:inline;> <a href="" style="color:white"> Education </li> <li style=display:inline;> <a href="" style="color:white"> Work Experience </li> <li style=display:inline;> <a href="" style="color:pink"> Hobbies </li> </ul> <article> <p> BLANK is a student at BLANK <br> He is currently enrolled in the Computer Information Systems <br> program at the school. He is expected to graduate in 2019. </p> </article>选项,第二个带有Client选项,而Location的{​​{1}}下拉更改则应添加一个js事件处理程序,例如jQuery:

Locations

在后端,将Client模型附加到$(document).on('change', '#clients', () => { ...call locations endpoint and update locations options... }) FK上,并为路径Location创建端点,在该端点上,您根据Client选择app/locations/<int:client_id>/:< / p>

Locations