Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
On click run query function script Django

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']
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!