下拉列表之间的同步:Django

发布于 2024-12-12 03:27:16 字数 537 浏览 0 评论 0原文

我有一个模型

class Location(models.Model):
    country = models.ForeignKey('LocationCountry',null = True,blank = True)
    city = models.ForeignKey('LocationCity',null = True,blank = True)
    locality = models.ForeignKey('LocationLocality',null = True,blank = True)

该模型的表格如下所示,

class LocationForm(ModelForm):
class Meta:
    model = Location

国家、城市和地区均为下拉列表。 我想做的是让它们像无线电选择一样,即如果我从国家/地区选择一个项目,那么所选的城市和地点项目应该变为空。 因此在任何情况下都只会选择其中 1 个。 问题清楚吗?任何帮助将不胜感激。

I have a model

class Location(models.Model):
    country = models.ForeignKey('LocationCountry',null = True,blank = True)
    city = models.ForeignKey('LocationCity',null = True,blank = True)
    locality = models.ForeignKey('LocationLocality',null = True,blank = True)

The form for this model is as follows

class LocationForm(ModelForm):
class Meta:
    model = Location

Th country,city and locality being FK come as drop down lists.
What i want to do is make them like radio select i.e. if i select an item from country then the city and locality item selected should become null. So at any instance only 1 of them is selected.
Is the question clear ? Any help will be highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛装女皇 2024-12-19 03:27:16

我认为(正如托尼在他的评论中提到的)你可以用 Javascript 实现这一点。有很多保管箱影响其他保管箱的示例(例如这个详尽的解释)。我建议您看一下该示例,看看是否可以将其塑造为您想要的行为。如果它不适合您,您也许应该重新表述您的问题,即您在下拉列表之间的同步方面遇到的确切问题。

I think (as tony mentioned in his comment) that you would get there with Javascript. There are plenty of examples of dropboxes influencing other dropboxes (like this thorough explanation). I suggest you take a look at that example, and see if you can mold it to the behavior you want. If it doesn't work out for you, you should perhaps rephrase your question the exact problem you are having with the synchronization between the dropdowns.

儭儭莪哋寶赑 2024-12-19 03:27:16
// jquery
$("select").change(function() {
     $("select").not(this).val('');
})
// jquery
$("select").change(function() {
     $("select").not(this).val('');
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文