[英]How to set prefilled input field active with Jquery/javascript?
I'm using a calculator widget where I can enter random values into an inputfield and then the widget automatically calculates when clicking the "go"-button. 我使用的是计算器小部件,可以在其中向输入字段输入随机值,然后在单击“执行”按钮时小部件会自动进行计算。
Now I want to insert/prefill the value into this input field, since the value which needs to be calculated comes from a server. 现在,我想将值插入/预填充到此输入字段中,因为需要计算的值来自服务器。 The issue though is, that this input field apparently only reacts on keypress.
但问题是,此输入字段显然仅在按键时起作用。 I tried to do this:
我试图这样做:
$('input[name="value"][data-type-money]').val('150.000').focus();
and 和
$('input[name="value"][data-type-money]').val('150.000').select();
which prefills the input field with the desired value but when I click the "go" button the calculation fails as long I dont enter the value manually into the input field. 这会在输入字段中预先填充所需的值,但是当我单击“执行”按钮时,计算失败,因为我没有手动在输入字段中输入值。 So, in the end my solution does not work.
因此,最终我的解决方案不起作用。
Does anyone know how I can solve this? 有谁知道我该怎么解决?
Edit, Updated 编辑,更新
the input field is - and reacts only on keypress, I mean when value is entered manually
输入字段是-并且仅在按键时做出反应,我的意思是当手动输入值时
If input
value
property cannot be changed , try replacing the element in DOM
with value set at html
using .replaceWith()
如果无法更改
input
value
属性,请尝试使用.replaceWith()
将DOM
的元素替换为html
设置的值。
$('input[name="value"][data-type-money]') .replaceWith("<input name=value data-type-money type=text value=150.00 />")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <input name="value" data-type-money type="text" />
If data changes frequently you can also use the setInterval function: 如果数据经常更改,您还可以使用setInterval函数:
<input name="value" data-type-money="money">
setInterval (function(){
var moneyValue = "150.000";
$('input[name="value"][data-type-money]').val(moneyValue);
},1000);
fiddle here: http://jsfiddle.net/85pbnnu1/ 在这里提琴: http : //jsfiddle.net/85pbnnu1/
or you can just do: 或者您可以执行以下操作:
$(document).ready(function(){
$('input[name="value"][data-type-money]').val('150.000').change();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.