【问题标题】:How can I focus input on a range slider with javascript?如何使用 javascript 将输入集中在范围滑块上?
【发布时间】:2021-01-02 09:12:45
【问题描述】:

我有输入,用户可以直接在其中输入值或使用范围滑块选择它。我想要的是,当用户选择范围滑块时,它会同时关注它控制的输入。

所以我有这些输入:

<label class="va1">Option a price 1:<input id="a1" type="number"/></label>
<input type="range" id="slider-a1" min="0" max="100" step="1" value="0"><br>

这个让范围滑块的值进入输入的js:

var opt_1 = document.getElementById("slider-a1");
opt_1.oninput = function() {
  document.getElementById("a1").value = opt_1.value;}

有没有办法让输入聚焦/不聚焦在范围滑块的使用上?

【问题讨论】:

    标签: javascript forms input rangeslider


    【解决方案1】:

    不,你不能,因为那只会将光标移到输入字段中,滑块会失去焦点,也就是说,你不能同时关注两个项目:

    示例:

    var opt_1 = document.getElementById("slider-a1");
    opt_1.oninput = function() {
      document.getElementById("a1").value = opt_1.value;
      document.getElementById("a1").focus();
      }
    .active  {
      border-color:red;
      border-style: inset;
      border-width: 1px;}
    <label class="va1">Option a price 1:<input id="a1" type="number"/></label>
    <input type="range" id="slider-a1" min="0" max="100" step="1" value="0"><br>

    所以我建议模仿焦点:

    var opt_1 = document.getElementById("slider-a1");
    opt_1.oninput = function() {
      document.getElementById("a1").value = opt_1.value;
      document.getElementById("a1").classList.add("active");
      }
    .active  {
      border-color:blue;
      border-style: inset;
      border-width: 2px;}
    <label class="va1">Option a price 1:<input id="a1" type="number"/></label>
    <input type="range" id="slider-a1" min="0" max="100" step="1" value="0"><br>

    编辑:

    如果您的输入计算由焦点控制(顺便说一句,他们可以在更改时进行,从我理解的情况来看,我认为女巫会更优化),您可以设置单独的事件,在滑块中鼠标向上将触发焦点根据您的意见;

    当您使用滑块时,焦点当然是在滑块上,但是一旦您释放它,您就可以切换到输入:

    示例:

    var opt_1 = document.getElementById("slider-a1");
    opt_1.oninput = function() {
      document.getElementById("a1").value = opt_1.value;
    }
    
    opt_1.onmouseup = function() {
      document.getElementById("a1").focus();
    }
    <label class="va1">Option a price 1:<input id="a1" type="number"/></label>
    <input type="range" id="slider-a1" min="0" max="100" step="1" value="0"><br>

    另外从评论中的其他链接我看到你使用 onfocusout 事件很难触发,所以我建议使用模糊:

    (但如果您只是简单地使用更改,所有这些似乎都是多余的......)

    var opt_1 = document.getElementById("slider-a1");
    opt_1.oninput = function() {
      document.getElementById("a1").value = opt_1.value;
    }
    
    
    opt_1.onmouseup = function() {
      document.getElementById("a1").focus();
      setTimeout(function() {
        console.log(true)
        opt_1.focus();
      }, 1000);
    }
    
    
    document.getElementById("a1").onblur = function() {
      console.log("blur out happend")
    };
    <label class="va1">Option a price 1:<input id="a1" type="number"/></label>
    <input type="range" id="slider-a1" min="0" max="100" step="1" value="0"><br>

    【讨论】:

    • 实际上,出于计算原因,我想同时关注它们。但如果你说不可能,那也没关系。无论如何,谢谢你的回答。
    • @mscmdaddcts 请更新您的问题,并提供您想要实现的目标的完整示例。也许您可以在滑块上的鼠标向上/释放事件后关注输入。我现在在手机上,但回家后可以做男性的例子。但它自己不能同时关注两个元素。你需要改变逻辑
    • 感谢您的回答 ikiK。是的,我认为这可能是一个解决方案,因为我的目标是当范围滑块聚焦/不聚焦时,输入的聚焦/不聚焦动作应该触发,而不是输入。我不知道是否清楚。而且,我之前问了一个问题,我删除了。这是该问题的链接,更详细:stackoverflow.com/q/63902809/13984348
    • @mscmdaddcts 我已经使用鼠标向上编辑了我的答案。
    • 非常感谢@ikiK,对我帮助很大!
    猜你喜欢
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多