需要使用javascript获取输入文本,然后将其添加到句子中

Need to get input text with javascript then add it to a sentence

本文关键字:然后 添加 句子 文本 javascript 输入 获取      更新时间:2023-09-26

这是我目前所需要的。我需要将数量输入框的值添加到输入区域后的句子中。

现在我有这个数量输入代码:

 <label for="quantity">Qty: </label> 
 <input min="1" type="number" id="quantity" name="quantity" value="1" onkeyup="getVals(this, 'text');"  />
 <input type="submit" id="add-to-cart" class="btn addtocart" name="add" value="Add to cart" />
 <div class="how-many">You have helped save <span id="pTextInput"></span>people</div>

我希望将id quantity的输入值插入span id="pTextInput"中。

所以基本上,如果输入的数量设置为2,那么"你已经帮助救了2个人"这句话就会出现(动态数字放在跨度中。

希望这是有道理的。我可以使用常规javascript或jquery,以最简单的为准。

如果我正确理解了这个问题,你可能正在寻找这样的东西:

<script type="text/javascript">
  $(function() {
    $("#pTextInput").html("1"); 
    $("#quantity").on("change keyup", function() {
      $("#pTextInput").html($(this).val());
    });
  });
</script>
 <label for="quantity">Qty: </label> 
 <input min="1" type="number" id="quantity" name="quantity" value="1" />
 <input type="submit" id="add-to-cart" class="btn addtocart" name="add" value="Add to cart" />
 <div class="how-many">You have helped save <span id="pTextInput"></span> people</div>

当您更改quantity的输入时,pTextInput的值将随之更改。

我想你想要这个:

function getVals(input) {
    document.getElementById("pTextInput").innerHTML = input.value;
}

此外,将getVals(this,'text')更改为getVals,不需要'text'。

我建议您看看JS的DOM操作基础知识:http://www.w3schools.com/js/js_htmldom.asp