Focus on slider after submitting form with slider

情到浓时终转凉″ 提交于 2019-12-24 13:07:04

问题


I am using the jquery ui slider and when the slider is slid, a form is submitted. The code is as follows:

<script type="text/javascript">
    $(function() {
        $( "#slider" ).slider({ 

                      min: 2000,
                      max: 2008,
                      value:<%=@epoch.start_year %>,
                      slide: function( event, ui ) {
                           $("#slider_value").html(ui.value);
                           $("form#new_epoch").submit(); 
                           }                          
        });

    });
</script>

When I move the slider to a different value, the form submits however when the page reloads the focus on the slider is lost. What I am looking to do is move the slider with the page updating everytime I move the slider, and with the slider remaining in focus. I am using the arrow keys to move the slider.


回答1:


Assuming you don't want to use ajax and load parts of HTML in the page, and you want to continue reloading the whole page, then you can set the focus on the slider handle as the page loads with $(".ui-slider-handle").focus();:

<script type="text/javascript">
$(function() {
    $( "#slider" ).slider({ 

                  min: 2000,
                  max: 2008,
                  value:<%=@epoch.start_year %>,
                  slide: function( event, ui ) {
                       $("#slider_value").html(ui.value);
                       $("form#new_epoch").submit(); 
                       }                          
    });
    $(".ui-slider-handle").focus();
});
</script>


来源:https://stackoverflow.com/questions/13692198/focus-on-slider-after-submitting-form-with-slider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!