Y
Y
Yura Khlyan2016-03-16 14:07:56
JavaScript
Yura Khlyan, 2016-03-16 14:07:56

How to reload a page using JS?

Good day.
There is a store (on Django-Oscar, but not so important), on the product page there is a dropdown menu that lists all the options for this product (packaging, etc.). It is necessary for me that after an element choice in this dropdown, the page of this product returned.
================== UPD ==================
And you can do this so that the page remembers which element was chosen? That is, on the page with the product, the corresponding element in the dropdown was selected?
========================================
Here is how the dropdown list is formed:

for child in child_products:
    summary = child.title
        ...
    choices.append((child.id, summary))
self.fields['child_id'] = forms.ChoiceField(
    choices=tuple(choices), label=_("Variant"),
    widget=widgets.AdvancedSelect(disabled_values=disabled_values))

Here is the url view:
url(r'^(?P<product_slug>[\w-]*)_(?P<pk>\d+)/$',
                self.detail_view.as_view(), name='detail')

I already figured it out with Django, but I haven’t mastered JS yet (but I plan to), I can’t solve it. Can you help?
I will be very grateful for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pshkll, 2016-03-16
@MAGistr_MTM

<select id="dynamic-select">
    <option value="http://www.google.com/">Google</option>
    <option value="http://www.youtube.com/">YouTube</option>
    <option value="http://www.yandex.ru/">Yandex</option>
</select>

<script>
    $('#dynamic-select').bind('change', function () {
        var url = $(this).val();
        if (url != '') {
            window.location = url;
        }
        return false;
    });
</script>

Additional answer to additional question in the comments, using GET:
<select id="dynamic-select">
    <option id="s1" value="test.html?s=s1">Google</option>
    <option id="s2" value="test2.html?s=s2">YouTube</option>
    <option id="s3" value="test3.html?s=s3">Yandex</option>
</select>

<script>
$(document).ready(function () {

    $.urlParam = function(name){
        var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
        if (results==null){
           return null;
        }
        else{
           return results[1] || 0;
        }
    }
    var selected = $.urlParam('s');
    if (selected){
        $("#dynamic-select option[id="+selected+"]").prop("selected", "selected");
    }

    $('#dynamic-select').bind('change', function () {
        var url = $(this).val();
        if (url != '') {
            window.location = url;
        }
        return false;
    });

});
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question