HTML5 input type range show range value

后端 未结 12 1294
野趣味
野趣味 2020-11-28 01:51

I am making a website where I want to use range slider(I know it only supports webkit browsers).

I have integrated it fully and works fine. But I would like to use a

相关标签:
12条回答
  • 2020-11-28 02:08

    version with editable input:

    <form>
        <input type="range" name="amountRange" min="0" max="20" value="0" oninput="this.form.amountInput.value=this.value" />
        <input type="number" name="amountInput" min="0" max="20" value="0" oninput="this.form.amountRange.value=this.value" />
    </form>
    

    http://jsfiddle.net/Xjxe6/

    0 讨论(0)
  • 2020-11-28 02:08

    If you're using multiple slides, and you can use jQuery, you can do the follow to deal with multiple sliders easily:

    function updateRangeInput(elem) {
      $(elem).next().val($(elem).val());
    }
    input { padding: 8px; border: 1px solid #ffffd; color: #555; display: block; }
    input[type=text] { width: 100px; }
    input[type=range] { width: 400px; }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <input type="range" min="0" max="100" oninput="updateRangeInput(this)" value="0">
    <input type="text" value="0">
    
    <input type="range" min="0" max="100" oninput="updateRangeInput(this)" value="50">
    <input type="text" value="50">

    Also, by using oninput on the <input type='range'> you'll receive events while dragging the range.

    0 讨论(0)
  • 2020-11-28 02:14

    I have a solution that involves (Vanilla) JavaScript, but only as a library. You habe to include it once and then all you need to do is set the appropriate source attribute of the number inputs.

    The source attribute should be the querySelectorAll selector of the range input you want to listen to.

    It even works with selectcs. And it works with multiple listeners. And it works in the other direction: change the number input and the range input will adjust. And it will work on elements added later onto the page (check https://codepen.io/HerrSerker/pen/JzaVQg for that)

    Tested in Chrome, Firefox, Edge and IE11

    ;(function(){
      
      function emit(target, name) {
        var event
        if (document.createEvent) {
          event = document.createEvent("HTMLEvents");
          event.initEvent(name, true, true);
        } else {
          event = document.createEventObject();
          event.eventType = name;
        }
    
        event.eventName = name;
    
        if (document.createEvent) {
          target.dispatchEvent(event);
        } else {
          target.fireEvent("on" + event.eventType, event);
        }    
      }
    
      var outputsSelector = "input[type=number][source],select[source]";
      
      function onChange(e) {
        var outputs = document.querySelectorAll(outputsSelector)
        for (var index = 0; index < outputs.length; index++) {
          var item = outputs[index]
          var source = document.querySelector(item.getAttribute('source'));
          if (source) {
            if (item === e.target) {
              source.value = item.value
              emit(source, 'input')
              emit(source, 'change')
            }
    
            if (source === e.target) {
              item.value = source.value
            }
          }
        }
      }
      
      document.addEventListener('change', onChange)
      document.addEventListener('input', onChange)
    }());
    <div id="div">
      <input name="example" type="range" max="2250000" min="-200000" value="0" step="50000">
      <input id="example-value" type="number" max="2250000" min="-200000" value="0" step="50000" source="[name=example]">
      <br>
    
      <input name="example2" type="range" max="2240000" min="-160000" value="0" step="50000">
      <input type="number" max="2240000" min="-160000" value="0" step="50000" source="[name=example2]">
      <input type="number" max="2240000" min="-160000" value="0" step="50000" source="[name=example2]">
      <br>
      
      <input name="example3" type="range" max="20" min="0" value="10" step="1">
      <select source="[name=example3]">
        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
        <option value="16">16</option>
        <option value="17">17</option>
        <option value="18">18</option>
        <option value="19">19</option>
        <option value="20">20</option>
      </select>
      <br>
      
    </div>
    <br>

    0 讨论(0)
  • 2020-11-28 02:14

    <form name="registrationForm">
        <input type="range" name="ageInputName" id="ageInputId" value="24" min="1" max="10" onchange="getvalor(this.value);" oninput="ageOutputId.value = ageInputId.value">
        <input type="text" name="ageOutputName" id="ageOutputId"></input>
    </form>

    0 讨论(0)
  • 2020-11-28 02:17

    Shortest version without form, min or external JavaScript.

    <input type="range" value="0" max="10" oninput="num.value = this.value">
    <output id="num">0</output>

    Explanation

    If you wanna retrieve the value from the output you commonly use an id that can be linked from the oninput instead of using this.nextElementSibling.value (we take advantage of something that we are already using)

    Compare the example above with this valid but a little more complex and long answer:

    <input id="num" type="range" value="0" max="100" oninput="this.nextElementSibling.value = this.value">
    <output>0</output>
    

    With the shortest answer:

    • We avoid the use of this, something weird in JS for newcomers
    • We avoid new concept about connecting siblings in the DOM
    • We avoid too much attributes in the input placing the id in the output

    Notes

    • In both examples we don't need to add the min value when equal to 0
    • Removing JavaScript’s this keyword makes it a better language
    0 讨论(0)
  • 2020-11-28 02:20

    an even better way would be to catch the input event on the input itself rather than on the whole form (performance wise) :

    <input type="range" id="rangeInput" name="rangeInput" min="0" max="20" value="0"
           oninput="amount.value=rangeInput.value">                                                       
    
    <output id="amount" name="amount" for="rangeInput">0</output>
    

    Here's a fiddle (with the id added as per Ryan's comment).

    0 讨论(0)
提交回复
热议问题