Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On select event Django templates

Tags:

html

django

Suppose I have dropdown field in PAGE A. Now on select event I want to redirect to PAGE B with some django context variables. The value of these context variable depends on the selected value of PAGE A dropdown field. How can I implement this in django?

like image 357
Asif Avatar asked Apr 07 '26 09:04

Asif


1 Answers

I think the best solution here is passing params in the query string to the view.

<div id="selector">
    <select>
        <option value="{% url pageB param1 %}">Option 1</option>
        <option value="{% url pageB param2 %}">Option 2</option>
        <option value="{% url pageB param3 %}">Option 3</option>
    </select>
</div>
<script>
    $(function(){
        // bind change event to select
        $('#selector select').bind('change', function () {
            var url = $(this).val(); // get selected value
            if (url) { // require a URL
                window.location = url; // redirect
            }
            return false;
         });
     });
 </script>
like image 153
Yasel Avatar answered Apr 09 '26 22:04

Yasel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!