简体   繁体   English

Chrome扩展程序:jQuery触发“ keyup”不会更新“ input”值-网站使用了剔除JS

[英]Chrome extension: JQuery triggering 'keyup' does not update the 'input' value - website uses knockout JS

I am writing an chrome extension on top of a website that I figured runs 'knockout JS'. 我在我认为运行“淘汰赛JS”的网站上编写了chrome扩展程序。

Consider the following HTML element: 考虑以下HTML元素:

<input type="text" class="form-control text-right" id="amount" autocomplete="off" data-bind="value: amount, valueUpdate: 'keyup'">

I am trying to write a bot that fills this field with a particular value. 我正在尝试编写一个用特定值填充此字段的机器人。 I have included JQuery as a dependency in the extension. 我已经将JQuery作为依赖项包含在扩展中。

I tried the following using the Jquery code. 我使用Jquery代码尝试了以下方法。 The objective is to update the value of the element to say 200. 目的是将元素的值更新为200。

$('#amount').val("200");
$('#amount').trigger("keypress");
$('#amount').trigger("keyup");

The code seems to update the value of the input visually, but the dependent knockout JS functions do not get triggered. 该代码似乎在视觉上更新了输入的值,但是依赖的敲除JS函数没有被触发。 I have not used Knockout JS before. 我以前没有使用过Knockout JS。

How do I trigger the knockout JS events ? 如何触发淘汰赛JS事件?

Update I hadn't paid attention that you were running a bot on the page. 更新我没有注意到您正在页面上运行机器人。 You should be able to get Knockout to recognize the change by 您应该能够通过淘汰赛来识别更改

$('#amount').val("200");
$('#amount').trigger('change');

 vm = { amount: ko.observable(100) }; ko.applyBindings(vm); vm.amount.subscribe(function(newValue) { alert("Changed"); }); setTimeout(function() { $('#amount').val(200); $('#amount').change(); }, 800); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <input id="amount" data-bind="value: amount" /> 

Original reply for anybody who might be writing Knockout 针对可能正在撰写淘汰赛的任何人的原始回复

You should probably go through the Knockout tutorial . 您可能应该阅读Knockout教程

If you're going to use Knockout, you will not be interacting directly with the DOM, as jQuery does. 如果要使用Knockout,则不会像jQuery那样直接与DOM交互。 Instead, you will manipulate variables in the viewmodel (generally speaking, the viewmodel is all the JavaScript code you write that is not attached to Knockout's ko object). 取而代之的是,您将在viewmodel中操作变量(通常来说,viewmodel是您编写的所有未附加到Knockout的ko对象上的JavaScript代码)。 In this case, the input's value is bound to the amount variable. 在这种情况下,输入值绑定到amount变量。 To change what's in the input, set the contents of the variable: 要更改输入内容,请设置变量的内容:

amount(200);

(Knockout uses "observable" variables, which are setter-getter functions, rather than simple values). (淘汰赛使用的是“可观察的”变量,这些变量是setter-getter函数,而不是简单的值)。

You can use below code to fulfill your requirement. 您可以使用下面的代码来满足您的要求。

<input type="text" class="form-control text-right" id="amount" autocomplete="off" data-bind="value: amount , valueUpdate: 'keyup'">

<p><span data-bind="text:amount"></span></p>
<script src="knockout-3.4.0.js"></script>

<script> 
    var obj = {amount:ko.observable('200')}
    ko.applyBindings(obj);
</script>

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

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