简体   繁体   English

如何使用Jquery / javascript将预填充的输入字段设置为活动状态?

[英]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.

相关问题 Javascript / jQuery:如何防止活动输入字段被更改? - Javascript / jQuery: How to prevent active input field from being changed? 如果预填了输入字段,则jQuery无法正常工作(切换清除功能) - jQuery not working if input field is prefilled (toggle clear function) 如何使用javaScript / jQuery将变量写入输入字段? - How to write a variable to an input field with javaScript/jQuery? 如何使我的输入字段预填充数据并在页面加载时可编辑? - How can I make my input field prefilled with data and editable on page load? 从 JavaScript 或 JQuery 设置 React 输入字段值 - Set React Input Field Value from JavaScript or JQuery 如何在输入文本字段或文本框中使用 javascript 或 JQuery 设置背景文本? - How set the Backgound Text using javascript or JQuery in the input text field or text box? Javascript设置输入字段的值 - Javascript set value of an input field Jquery自动完成键盘导航设置输入值,如何更改字段? - Jquery autocomplete keyboard navigation set the Value in input, how to change field? 如何使用Jquery设置隐藏输入字段的值? - How can I set the value of a hidden input field using Jquery? 如何将输入字段设置为空,在jquery中用作自动完成? - How to set input field to empty which is used as autocomplete in jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM