使用滑块按钮更改输入字段类型

问题描述 投票:0回答:1

我正在尝试使用滑块按钮将输入类型从“文本”更改为“选择”。但是,Javascript 代码没有响应。由于我对 Javascript 比较陌生,我不确定是什么导致了错误。因此,任何帮助将不胜感激!

这是 HTML 代码:

<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#offerModal">Open Modal</button>
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="offerModalLabel">Offer</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="switchItems">
item1
<label class="switch" >
<input type="checkbox" id="switchClick">
<span class="slider round"></span>
</label>
list1
</div>
<div class="input-group">
<input type="text" class="form-control" placeholder="state a price..." name='Message' id="message">
</div>
<br>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" value="send">
</div>
</div>

这是CSS文件:

.switch {
    position: relative;
    display: inline-block;
    width: 60px;
    height: 34px;
  }
  
  .switch input { 
    opacity: 0;
    width: 0;
    height: 0;
  }
  
  .slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #0EEEFC;
    -webkit-transition: .4s;
    transition: .4s;
  }
  
  .slider:before {
    position: absolute;
    content: "";
    height: 26px;
    width: 26px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    -webkit-transition: .4s;
    transition: .4s;
  }
  
  input:checked + .slider {
    background-color: #84FF79;
  }
  
  input:focus + .slider {
    box-shadow: 0 0 1px #ccc;
  }
  
  input:checked + .slider:before {
    -webkit-transform: translateX(26px);
    -ms-transform: translateX(26px);
    transform: translateX(26px);
  }
  
  .slider.round {
    border-radius: 34px;
  }
  
  .slider.round:before {
    border-radius: 50%;
  }

这是js代码:

$("#switchClick").on('click',function() {
    if ($("#message").attr("type") == "text") {
        $("#message").attr("type", "select");
    } else {
        $("#message").attr("type", "text");
    }
  });

我尝试参考网上的一些解决方案,但没有任何效果。

javascript
1个回答
0
投票

输入类型选择不存在。 请参阅官方文档以了解可用的类型。 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input

我添加了一个代码片段,将输入转换为按预期工作的范围。 另外,如果你真的不需要的话,我会尽量不使用 jQuery。

var elem = document.getElementById('switchClick');
elem.addEventListener('click', ChangeType);

function ChangeType() {
  var elem = document.getElementById('message');
  if (elem.type == 'text') {
    elem.type = 'range';
  } else {
    elem.type = 'text'
  }
}
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #0EEEFC;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked+.slider {
  background-color: #84FF79;
}

input:focus+.slider {
  box-shadow: 0 0 1px #ccc;
}

input:checked+.slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#offerModal">Open Modal</button>
<div class="modal-content">
  <div class="modal-header">
    <h5 class="modal-title" id="offerModalLabel">Offer</h5>
    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
  </div>
  <div class="modal-body">
    <div class="switchItems">
      item1
      <label class="switch">
<input type="checkbox" id="switchClick">
<span class="slider round"></span>
</label> list1
    </div>
    <div class="input-group">
      <input type="text" class="form-control" placeholder="state a price..." name='Message' id="message">
    </div>
    <br>
    <div class="modal-footer">
      <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
      <input type="submit" class="btn btn-primary" value="send">
    </div>
  </div>
</div>

© www.soinside.com 2019 - 2024. All rights reserved.