【问题标题】:How to display the value of a "number type input" in a span next to it如何在旁边的跨度中显示“数字类型输入”的值
【发布时间】:2019-09-07 00:50:17
【问题描述】:

我有一个数字输入类型的元素。我希望它的值显示在它旁边的跨度中。当我单击或滚动输入内的增加或减少按钮时。我希望跨度的相对值发生变化。可以吗??

我尝试获取值并在 span 中显示,但没有成功。

Javascript

var displayer = document.getElementById('displayer');
var day = document.getElementById('days');
var hrs = document.getElementById('hours');
var s_t = document.getElementById('s_t');

function funcday() {
  day.style.border = "1.5px solid darkorange";
  hrs.style.border = "1.5px solid mediumaquamarine";
  s_t.style.border = "1.5px solid darkorange";
  s_t.setAttribute("max", 365);
  var d = s_t.value;
  document.getElementById('display_d').textContent = d;
}

function funhrs() {
  day.style.border = "1.5px solid mediumaquamarine";
  hrs.style.border = "1.5px solid darkorange";
  s_t.style.border = "1.5px solid darkorange";
  s_t.setAttribute("max", 48);
  var h = s_t.value;
  document.getElementById('display_h').textContent = h;
}
#container {
  display: flex;
}

.st {
  width: 60px;
  margin-right: 10px;
}

#days,
#hours {
  color: #fff;
  width: 70px;
  margin-left: 10px;
  padding: 6px;
  cursor: pointer;
  background-color: mediumaquamarine;
  border: 1.5px solid mediumaquamarine;
  text-align: center;
  font-family: 'Courier';
}

.displayer {
  align-content: center;
  text-align: center;
  line-height: 2;
  margin-left: 20px;
  margin-right: 20px;
  font-size: 15px;
  border: 1px solid white;
  display: block;
  float: right;
}

.displayer span {
  margin-right: 5px;
  margin-left: 5px;
}
<div id="container">
  <input type="number" min="1" class="st" id="s_t" autocomplete="off">
  <div id="days" onclick="funcday();">Days</div>
  <div id="hours" onclick="funhrs();">Hours</div>
  <div id="displayer" class="displayer">
    <span id="display_d">00</span><span>Days</span>
    <span id="display_h">00</span><span>Hours</span>
  </div>
</div>

【问题讨论】:

  • 您的 textContent 分配不是分配,只需使用一个相等。 document.getElementById('display_h').textContent = h;
  • 这个问题是由无法再复制的问题或简单的印刷错误引起的。虽然类似的问题可能是这里的主题,但这个问题的解决方式不太可能帮助未来的读者。

标签: javascript html css


【解决方案1】:

您应该将 onchange 事件处理程序添加到输入元素并在此处理程序内更改 span 元素的文本。

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 2020-07-05
    • 2018-03-26
    • 2013-03-17
    • 1970-01-01
    相关资源
    最近更新 更多