简体   繁体   中英

Passing value from a jquery slider into Django views.py

How can I pass a value selected from a jQuery slider into Django views.py. Namely I've made a slider using (which probably needs to be edited to do what I want):

$(function() {
 $( ".slider").slider({
  min: 0,
  max: 1,
  value: 0.3,
  step: 0.05,
 })
});

with the relevant html:

<form method="get"> 
<div name="slider" class="slider"></div>
<input type="submit" value="submit">
</form>

When the user hits the submit button I would like to pass the value in the slider to views.py using something like:

value = request.GET['slider']

I've seen similar questions online, however, I'm new to web development and therefore don't quite understand how to adapt them to my case.

The slider isn't attached to a form control so it won't get passed to the server. Try:

$(function() {
 $( ".slider").slider({
  min: 0,
  max: 1,
  value: 0.3,
  step: 0.05,
  slide: function(event, elt) {
    $("#slider_control").val(elt.value);
  }
 })
});

Also add:

<input type="hidden" name="slider_control" id="slider_control" />

You'll get the value in as slider_control -- you can modify if you wish, of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM