简体   繁体   English

在Django中,如何使选择下拉选项自动提交表单?

[英]In Django, how to make selecting a drop-down option automatically submit a form?

I'm trying to implement a form which pre-populates fields based on another field. 我正在尝试实现一种表单,该表单根据另一个字段预先填充字段。 As a starting point, I'd like to make the form 'auto-submit' when an option from a drop-down menu is selected. 首先,我希望在从下拉菜单中选择一个选项后,将表单设置为“自动提交”。 I've tried the following template: 我尝试了以下模板:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    $(".auto-submit").change(function() {
        $(this).closest("form").submit();
    });
</script>


<form action="" method="post">{% csrf_token %}
    {% for field in form %}
        {% if field.name == "checkin_type" %}
            <div class="auto-submit">
                {{ field.errors }}
                {{ field.label_tag }}
                {{ field }}
            </div>
        {% else %}
            <div>
                {{ field.errors }}
                {{ field.label_tag }}
                {{ field }}
            </div>
        {% endif %}
    {% endfor %}
    <input type="submit" value="Send message" />
</form>

where the view is based on Django's generic CreateView : 该视图基于Django的通用CreateView

from django.views import generic
from .models import CheckIn


class CheckInCreate(generic.CreateView):
    model = CheckIn
    fields = '__all__'

and the models are 和模型是

from django.db import models
from django.urls import reverse


class CheckInType(models.Model):
    title = models.CharField(blank=True, max_length=255)
    description = models.TextField(blank=True)

    def __str__(self):
        return self.title


class CheckIn(models.Model):
    checkin_type = models.ForeignKey(CheckInType, null=True, on_delete=models.CASCADE)
    title = models.CharField(blank=True, max_length=255)
    description = models.TextField(blank=True)

    notes = models.TextField(blank=True)

    # Scheduling
    requested_date = models.DateField(blank=True, null=True)
    completed_date = models.DateField(blank=True, null=True)

    def get_absolute_url(self):
        return reverse('checkin-detail', kwargs={'pk': self.id})

    def save(self, *args, **kwargs):
        if self.checkin_type:
            if not self.title:
                self.title = self.checkin_type.title
            if not self.description:
                self.description = self.checkin_type.description
        super().save(*args, **kwargs)

However, if I actually select a drop-down menu option in the browser, nothing happens. 但是,如果我实际上在浏览器中选择了一个下拉菜单选项,则什么也不会发生。 Can someone explain why this is not working? 有人可以解释为什么这行不通吗?

You're trying to attach the event handler before the drop down menu is loaded into the DOM. 您正在尝试在下拉菜单加载到DOM之前附加事件处理程序。 You can use document.ready to wait until it is loaded to attach the handler 您可以使用document.ready等待加载完成后附加处理程序

$(document).ready(function(){
    $(".auto-submit").change(function() {
        $(this).closest("form").submit();
        //this.form.submit(); //less verbose
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从SuiteScript的下拉菜单中选择选项后,如何显示文本框? - How do I make a text box appear after selecting an option from a drop-down menu in SuiteScript? (Django)如何在更改外键下拉列表后使“选定”选项有效? - (Django) How do I make the 'selected' option valid after altering a foreign key drop-down? Bootstrap 下拉提交表单 - Bootstrap drop-down submit form 如何使用依赖的下拉列表Django + JS进行搜索? - How to make search with dependent drop-down lists Django + JS? 选择下拉框选项不会影响页面 - Selecting a drop-down box option does not affect page 选择包含HTML的变量的下拉选项 - selecting drop-down option of variable which contain html 下拉重定向-提交 - Drop-down Redirect - Submit 表单提交后,动态相关下拉列表中没有选项 - No option in a dynamic dependant drop-down list after form submission 如何将我的选项设置为默认选项,因此如果我清除下拉菜单,默认情况下将填充该选项 - How to make my option a default so if I clear my drop-down it is populated by the default 如何在我的选择下拉列表中每隔2秒按数字选项项进行多次自动更改 - How to make multiple auto on change by number option item in my select drop-down every 2 seconds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM