javascript - 如何将 JavaScript 值传递到输入字段

标签 javascript html

我编写了移动范围按钮的代码,数量将增加跨度 ID 的值,我也通过 JavaScript 做到了这一点,但我尝试在输入字段内获取相同的值,我无法理解问题是什么请帮我解决这个问题。

var output = document.getElementById("demo");

document.getElementById("demo").value = output;

output.innerHTML = slider.value;

slider.oninput = function() {
    output3.innerHTML = this.value;
    output.innerHTML = this.value;

 output2.innerHTML = this.value;
}
<div class="col-md-12">
  <p>Loan Amount<span id="demo3"></span></p>
  <input type="range" min="5000" max="50000" value="1000" class="slider" id="myRange" step="1000">
</div>


<div class="col-md-12">
  <p>Loan Period</p>
  <input type="range" min="10" max="50" class="slider" id="myRange2" step="10" oninput="getInput(this.value)">
</div>


<div class="col-md-4">
  <img src="./assets/img/cal-01.png" style=" max-width: 48%; " />
  <p>Loan Amount:<br/><span id="demo"></span></p>
</div>

<div class="col-md-4">
  <img src="./assets/img/cal-02.png" style=" max-width: 37%; " />
  <p>Loan Period:<br/><span id="demo4"></span> Days</p>
</div>


<input type="text" class="form-control" id="demo" name="demo">

我想将范围 ID 值传递给输入字段 ID

最佳答案

首先,html 文档中任何两个元素都不应该具有相同的 id,因此您必须将 id 为“demo”的元素之一更改为唯一的元素。

然后使用.textContent获取span元素上的内容。

var output = document.getElementById("span-demo");  // this is the span element

document.getElementById("demo").value = output.textContent; // put the text context inside the value of the input element.

output.innerHTML = slider.value;

slider.oninput = function() {
    output3.innerHTML = this.value;
    output.innerHTML = this.value;

 output2.innerHTML = this.value;
}

关于javascript - 如何将 JavaScript 值传递到输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54635938/

相关文章:

javascript - 使用 axios 获取访问 token

html - 将一个 div(以及其他)扩展到父容器之外

javascript - Vue中双向绑定(bind)props的正确方式是什么?

javascript - 在html中添加img src标签的点击事件

html - 图片超出 div 标签

php - 将行标记为重要并在每次加载时保存它

javascript - 最后一个参数是回调时的NodeJS默认参数

php - 防止表单提交时重定向并在 php 中使用 POST 变量

javascript - 如何将输入栏推开另一个?

javascript - jQuery 双击不在表行上工作