简体   繁体   English

使用JavaScript / jquery刷新文本字段中的值

[英]Refresh value in text field with JavaScript/jquery

I have a function that calculate price for a product. 我有一个计算产品价格的功能。 I'm not JavaScript developer so my knowledge is very limited. 我不是JavaScript开发人员所以我的知识非常有限。

By changing the value in the text field script calculate price for product. 通过更改文本字段脚本中的值来计算产品的价格。

 <input type="text" value="" name="options[22]" class="input-text"
 onfocus="opConfig.reloadPrice()">

Problem is that the script triggers only if the following is done: 问题是只有在完成以下操作后脚本才会触发:

  1. insert value into the textfield 将值插入文本字段
  2. click somewhere outside the textfield 单击文本域外的某个位置
  3. click back into the text field 单击返回文本字段

All I need is a button saying refresh that by clicking it will have functionality of step 2 and step above. 我只需要一个按钮说刷新,通过点击它将具有上面步骤2和步骤的功能。

I'm not sure if I explained it properly so if there is any more information required to resolve this issue please let me know. 我不确定我是否正确解释,如果有更多信息需要解决此问题,请告诉我。

Here is the link to the site. 这是该网站的链接。

http://www.floorstodoors.mldemo.co.uk/spotlight/oak-value-lacquered-3-strip.html http://www.floorstodoors.mldemo.co.uk/spotlight/oak-value-lacquered-3-strip.html

The field im trying to amend/add refresh button is Enter Square Metre 我试图修改/添加刷新按钮的字段是Enter Square Meter

You'd add your event to a button, and retrieve a reference to your input by assigning an ID: 您将事件添加到按钮,并通过分配ID来检索对输入的引用:

<input type="text" value="" name="options[22]" id="price" class="input-text" />
<input type="button" value="Refresh" onclick="reloadPrice();" />

function reloadPrice() {
    var price = "0.00"; // set your price here

    // get a ref to your element and assign value
    var elem = document.getElementById("price");
    elem.value = price;
}

I'm not sure I fully understand you, but is this what you need? 我不确定我完全理解你,但这是你需要的吗?

<input type="text" value="" name="options[22]" class="input-text">
<input type="button" onclick="opConfig.reloadPrice()" value="Refresh" />

A button with an click-event listener, so that when you click the refresh-button the opConfig.reloadPrice() method gets executed. 一个带有单击事件监听器的按钮,这样当您单击刷新按钮时,将执行opConfig.reloadPrice()方法。

Edit based on comment: 根据评论进行编辑:

I'm not sure what JavaScript library you are using, but you have these two lines in you code that seems to add event-listeners to the input with id qty . 我不确定你使用的是哪个JavaScript库,但是你的代码中有这两行,它们似乎将事件监听器添加到id为qty的输入中。

$('qty').observe('focus',function(){
    $('qty').setValue($('qty').getValue().replace(/[^0-9]/g,''));
});     
$('qty').observe('focus',this.getFromQties.bind(this))

They are listening for the focus event, thus only triggers when your input field gains focus. 他们正在侦听focus事件,因此只有在您的输入字段获得焦点时才会触发。

If you modify those to listen for the keyup event instead, I believe it will work better. 如果你修改那些来监听keyup事件,我相信它会更好。 Without being familiar with the framework, I guess the only thing to change would be this: 在不熟悉框架的情况下,我想唯一要改变的是:

$('qty').observe('keyup',function(){
    $('qty').setValue($('qty').getValue().replace(/[^0-9]/g,''));
});     
$('qty').observe('keyup',this.getFromQties.bind(this))

使用onchange或onblur而不是onfocus!

use onchange. 使用onchange。 This will activate anytime the value changes: 这将在值更改时随时激活:

<input type="text" value="" name="options[22]" class="input-text" onchange="opConfig.reloadPrice()">

First: this is JavaScript and not Java - so you have to be a javascript and not a java developer. 第一:这是JavaScript而不是Java - 所以你必须是一个javascript而不是java开发人员。

to solve your problem you can make a new button with a onclick attribute and execute your function there which you have in your onfocus attribute in the text-field. 要解决您的问题,您可以创建一个带有onclick属性的新按钮,并在文本字段中的onfocus属性中执行您的函数。 or you can take another event - like onchange or onblur for instance.. 或者你可以采取另一个事件 - 例如onchange或onblur ..

<input type="text" onchange="..yourfunctionhere...">

http://www.w3schools.com/js/js_events.asp http://www.w3schools.com/js/js_events.asp

All I need is a button saying refresh that by clicking it will have functionality

For that you need a button with onclick listener, do the below things. 为此你需要一个带onclick监听器的按钮,做下面的事情。

<input type="button" value="Refresh" onclick="opConfig.reloadPrice();" />

<input type="text" value="" name="options[22]" class="input-text"/>

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

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