Javascript-форум (https://javascript.ru/forum/)
-   Элементы интерфейса (https://javascript.ru/forum/dom-window/)
-   -   jQuery UI Slider - привязка с input type number (https://javascript.ru/forum/dom-window/69409-jquery-ui-slider-privyazka-s-input-type-number.html)

Argeares 20.06.2017 14:36

jQuery UI Slider - привязка с input type number
 
Здравствуйте. Помогите пожалуйста, не могу никак сделать что бы ползунок был привязан с input type number. Что бы при движении ползунка менялось значение в input, а когда меняешь значение в input что бы двигался соответственно ползунок. Смотрел на офф сайте, там только с селектом так есть вот ссылка https://jqueryui.com/slider/#hotelrooms.

Вот как сделал я, но так не работает. Подскажите как правильно сделать пожалуйста

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Slider - Slider bound to select</title>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="https://resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
 
<form id="reservation">
  <input type="number" step="1" min="1" max="27" id="minbeds" name="minbeds" size="4" value="0">
</form>
 
<script>
  $( function() {
    var select = $( "#minbeds" );
    var slider = $( "<div id='slider'></div>" ).insertAfter( select ).slider({
      min: 1,
      max: 27,
      range: "min",
      value: select[ 0 ].selectedIndex + 1,
      slide: function( event, ui ) {
        select[ 0 ].selectedIndex = ui.value - 1;
      }
    });
    $( "#minbeds" ).on( "change", function() {
      slider.slider( "value", this.selectedIndex + 1 );
    });
  } );
</script>

</body>
</html>

рони 20.06.2017 15:17

Argeares,
:-?
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Slider - Slider bound to select</title>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="https://resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>

<form id="reservation">
  <input type="number" step="1" min="1" max="27" id="minbeds" name="minbeds" size="4" value="1">
</form>

<script>
  $( function() {
    var select = $( "#minbeds" );
    var slider = $( "<div id='slider'></div>" ).insertAfter( select ).slider({
      min: 1,
      max: 27,
      range: "min",
      value: select[ 0 ].value ,
      slide: function( event, ui ) {
        select[ 0 ].value = ui.value;
      }
    });
    $( "#minbeds" ).on( "change", function() {
      slider.slider( "value", this.value );
    });
  } );
</script>

</body>
</html>

Argeares 20.06.2017 15:25

Спасибо

j0hnik 20.06.2017 15:36

и лучше вот так, чтоб слайдер не тормозил
$( "#minbeds" ).on( "input", function() {
      slider.slider( "value", this.value );
    });


Часовой пояс GMT +3, время: 10:08.