简体   繁体   English

如何自动填写淘汰表

[英]How to automate filling out knockout form

Hello I currently have a site with the following section in my chrome dev tools 您好,我目前在我的chrome开发工具中有一个包含以下部分的网站

<div class="itemEditorField-inputsRelation">
   <input class="details-relativeInput-ageOnset js-relativeInput-ageOnset" type="number" data-bind="numeric: ageOnset, attr: { min: 0, max: 999, placeholder: 'Age' }" min="0" max="999" placeholder="Age"></input>
</div>

How do I simulate entering text into the knockoutJS input field? 我如何模拟在淘汰表输入字段中输入文本? I have tried ... 我努力了 ...

e = $.Event('keydown');e.keyCode=50;$('input.details-relativeInput-ageOnset.js-relativeInput-ageOnset').trigger(e);

but all it returns is the object itself. 但是它返回的只是对象本身。 I do not have the code itself to plug anything into. 我没有代码本身可以插入任何东西。 I just have Chrome and JQuery to use. 我只有Chrome和JQuery可以使用。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

If you had a normal value binding, the trick would be firing the change trigger for the input field. 如果您具有普通value绑定,则技巧将是触发输入字段的change触发器。 This might work for you, but it's hard to say, since you have a numeric binding, which is not a standard binding handler, but a custom one. 这可能对您有用,但是很难说,因为您有一个numeric绑定,它不是标准的绑定处理程序,而是自定义的处理程序。 Whatever event causes the bound variable to be updated is what you want to trigger. 无论什么事件导致绑定变量被更新,这都是您要触发的。

A demo with a value binding is below. 下面是带有value绑定的演示。

 var vm = { ageOnset: ko.observable('') }; vm.ageOnset.subscribe(function(newValue) { alert("New value:" + newValue); }); ko.applyBindings(vm); setTimeout(function () { $('input').val('12').change(); }, 500); 
 <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> <div class="itemEditorField-inputsRelation"> <input class="details-relativeInput-ageOnset js-relativeInput-ageOnset" type="number" data-bind="value: ageOnset, attr: { min: 0, max: 999, placeholder: 'Age' }" min="0" max="999" placeholder="Age" /> </div> 

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

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