1

In my Django project, on my HTML page, I have a script which runs Query depending on the other values ($("#id_report").val() values). Function runs 'on click'. Problem is, when I click on '#id_person' dropdown menu to select an option which I get from query, I run query again and my selection gets reset.

Problem is:

  1. I click on dropdown menu
  2. Query runs
  3. I select one of the options from Query return data
  4. When I select, I need to click a selection, Query runs again and my selection is lost I need the script to run only once and dropdown menu is twice a click. First to selet dropdown, second to select an option.

post_form.html

{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">

        <form method="POST" id="PostForm" data-sektor-url="{% url 'ajax_load_sektors' %}" data-department-url="{% url 'ajax_load_departments' %}"  data-person-url="{% url 'ajax_load_persons' %}" novalidate enctype="multipart/form-data">
            {% csrf_token %}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Submit report</legend>
                {{ form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Report</button>
            </div>
        </form>
    </div>
    <script>
        $("#id_person").click(function () {
            var value = $("#id_report").val();
            var url = $("#PostForm").attr("data-person-url");
            $.ajax({                      
                url: url,    
                data: {
                'value': value,
                },
                success: function (data) { 
                    $("#id_person").html(data);  
                    console.log(data);
                }
            });
        });
    </script>
{% endblock content %}

How can I run this only when I click on dropdown, not when I select (then it runs again because it's a click).

forms.py

from django import forms
from .models import Post, File

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['title', 'subject', 'person', 'report']
4
  • 1
    Are you sure that this question is about python and django? Commented Nov 19, 2021 at 13:20
  • is your dropdown from a foreignkey and could you please show your full form ? Commented Nov 19, 2021 at 13:23
  • Edited. It's more under 'HTML DOM events JavaScript'. Thank you for pointing that out. Commented Nov 19, 2021 at 13:23
  • Sorry but I don't know how to help you. Try posting under the HTML, DOM or JQUERY tags Commented Nov 19, 2021 at 13:25

1 Answer 1

1

try this .change

<script>
    $("#id_person").change(function () {
        var value = $("#id_report").val();
        var url = $("#PostForm").attr("data-person-url");
        $.ajax({                      
            url: url,    
            data: {
            'value': value,
            },
            success: function (data) { 
                $("#id_person").html(data);  
                console.log(data);
            }
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

8 Comments

It does not work. Already tried. Problem is: 1. I click on dropdown menu 2. Query runs 3. I select one of the options from Query return data 4. When I select Query runs again and my selection is lost I need the script to run only once. And dropdown menu is twice a click. First to selet dropdown, second to select an option.
what is the problem ? and is this report from a foreignkey ?
@MarkoZg i think i see what you mean could you please show your forms.py for that form.
@MarkoZg does this dropdown dependent between report and person ?
Yes. Depending what is selected in 'Report', a different data will be writen to 'Person'. So when is writen I need to select a Person. It's not an option to write to 'Person' when 'Report' is changed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.