jQuery slider onChange auto submit form

Posted by bikey77 on Stack Overflow See other posts from Stack Overflow or by bikey77
Published on 2012-11-16T10:47:45Z Indexed on 2012/11/16 11:00 UTC
Read the original article Hit count: 199

Filed under:
|

I'm using a jQuery slider as a price range selector in a form. I'd like to have the form submit automatically when one of the values has been changed. I used a couple of examples I found on SO but they didn't work with my code.

<form action="itemlist.php" method="post" enctype="application/x-www-form-urlencoded" name="priceform" id="priceform" target="_self">    
  <div id="slider-holder">
    Prices: From <span id="pricefromlabel">100 &#8364;</span> 
              To <span id="pricetolabel">500 &#8364;</span>
    <input type="hidden" id="pricefrom" name="pricefrom" value="100" />
    <input type="hidden" id="priceto" name="priceto" value="500" />
    <div id="slider-range"></div>
    <input name="Search" type="submit" value="Search" />
  </div>
</form>

This is the code that displays the values of the slider and updates 2 hidden form fields I use to store the prices in order to submit:

<script>

    $(function() {
            $("#slider-range" ).slider({
                range: true,
                min: 0,
                max: 1000,
                values: [ <?=$minprice?>, <?=$maxprice?> ],
            start: function (event, ui) {
                    event.stopPropagation();
                },
                slide: function( event, ui ) {
            $( "#pricefrom" ).val(ui.values[0]);
            $( "#priceto" ).val(ui.values[1]);
            $( "#pricefromlabel" ).html(ui.values[0] + ' &euro;');
            $( "#pricetolabel" ).html(ui.values[1] + ' &euro;');
        }
            });
        return false;
        });

</script>

I've tried adding this code as well as an data-autosubmit="true" attribute to the div but no result.

$(function() {
  $('[data-autosubmit="true"]').change(function() {        
    parentForm = $(this).('#priceform');
    clearTimeout(submitTimeout);
    submitTimeout = setTimeout(function() { parentForm.submit() }, 100);        
  });

I've also tried adding a $.post() event to the slider but I'm not very good with jQuery so I'm probably doing it wrong. Any help will be appreciated.

© Stack Overflow or respective owner

Related posts about jquery-ui

Related posts about form-submit