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

时间:2019-02-11 17:25:19

标签: 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

1 个答案:

答案 0 :(得分:1)

首先,在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;
}