简体   繁体   English

如何触发Span元素填充在“输入”字段(Javascript)中输入的值

[英]How to trigger the Span element to populate with the value entered in Input field (Javascript)

I'm creating a Bookmarklet for a webpage and have come across a Span id="totalAmount" element (summary table) that normally populates after a value is manually entered into an Input field (id="Amount") by clicking into it and typing the value. 我正在为网页创建一个Bookmarklet,并遇到一个Span id =“ totalAmount”元素(摘要表),该元素通常是在将值手动输入到“输入”字段(id =“ Amount”)中并单击后填充的,输入值。 I can successfully enter the value using the Bookmarklet but the Span id="totalAmount" element is not being populated. 我可以使用“书签”成功输入值,但未填充Span id =“ totalAmount”元素。 What am i missing? 我想念什么?

This is the HTML 这是HTML

<input id="Amount" class="with-prefix sum-amount" type="tel" value="" name="Amount" data-val-required="Please enter your amount" data-val-regex-pattern="^\d*\.?\d{0,2}$" data-val-regex="Please enter the amount" data-val-reasonableincome="" data-val-number="The field What is your amount? must be a number." data-val="true" aria-describedby="AmountTooltip"/>

<div class="summary-table clearfix">
<div class="summary-table-item">
<span class="text">Total amount</span>
<span class="curr-sign">£</span>
<span id="totalAmount" class="value"/>
</div>

When entering value into Input field manually, the span element gets the same value populated and the code changes to: 手动在“输入”字段中输入值时,span元素将填充相同的值,并且代码更改为:

<span id="totalAmount" class="value">25000.00</span>

But when I use following Bookmarklet, although the Input field is successfully entered with a value, the "totalAmount" Span isn't getting populated 但是,当我使用以下Bookmarklet时,尽管已使用值成功输入了Input字段,但“ totalAmount”跨度并未填充

javascript: var newPage = {
    formFill: function() {
        document.querySelectorAll("#Amount")[0].value = '25000';
    }
};
newPage.formFill()

NOTE : I'm unable to modify the HTML due to permissions 注意 :由于权限的原因,我无法修改HTML

Much appreciated 非常感激

span is not the input element, it has innerHTML , not value span不是input元素,它具有innerHTML ,而不是value

document.getElementById( "Amount" ).onchange = function(){
   document.getElementById("totalAmount").innerHTML = this.value;
}

Firstly the span is not closed properly. 首先,跨度未正确关闭。 Please correct that. 请更正。

The query is happening on Input element and not span. 查询发生在Input元素上,而不是跨度。

And if you want the span to populate the value entered in the input element. 如果希望跨度填充在输入元素中输入的值。 You have to write a function that does the calculation. 您必须编写一个进行计算的函数。 This can be bound to a keyboard event. 这可以绑定到键盘事件。

// use this code and close span tag.

javascript: var newPage = {
  formFill: function() {
     document.querySelectorAll("#Amount")[0].value  = '25000';
     document.getElementsByClassName(".curr-sign").textContent = '25000';
     document.getElementById("totalAmount").textContent = "25000";
  }
};
newPage.formFill();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM