使用 Knockout 控制网页:未保存可见更改 [英] Controls Webpage with Knockout: Visible changes not saved

查看:25
本文介绍了使用 Knockout 控制网页:未保存可见更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 Excel VBA 编辑网站.编辑似乎有效,但当我使用保存按钮时,没有保存任何内容.为什么屏幕上可见的更新数据没有被保存?

Trying to edit a website with Excel VBA. The edits appear to work, but when I use the save button, nothing is saved. Why isn't updated data, which is visible on the screen, being saved?

这段代码在 Internet Explorer 中打开一个网页,导航到我想要的地方,填写数据,所有这些都显示在屏幕上,使用各种方法,例如:

This code opens a web page in internet explorer, navigates where I want, fills out data, all which show on the screen, using various methods, such as:

For Each objElement In objElementColl
ExtractedName = objElement.outerHTML
        
If InStr(ExtractedName, "NewPermit") > 0 Then
objElement.Checked = True

Set DropDown = objHTML.getElementById("ProjectFile-AccreditedCertifierId")
DropDown.selectedIndex = 1

objHTML.getElementsByName(ElementName)(0).Value = ValueCheck

所有工作和更改都显示在屏幕上.我点击保存使用:

All work and changes appear on the screen. I click save by using:

Set objElementColl = objHTML.getElementsByClassName("btn")

For Each objElement In objElementColl
    ExtractedName = objElement.outerHTML
    
    If InStr(ExtractedName, "click: save, enable:") > 0 Then
        objElement.Click
        ExtractedName = 1
        Exit For
    End If
Next

哪个运行.问题是它没有保存以上三部分的更改.

Which runs. The issue is it doesn't save the changes from the three pieces above.

我所尝试的

  1. 暂停我的代码并手动点击保存(同样的问题)

  1. Pause my code and manually click save (same issue)

暂停我的代码,手动更改复选框并运行代码以保存(确实保存手动更改,但不保存编码的更改

Pause my code, manually change a checkbox and run the code to save (does save the manual change, but not the coded ones

暂停代码并手动更改一个框并手动保存(仅保存手动更改的框)

Pause the code and manually change a box and manually save (only manually changed box is saved)

从上面看,我的保存点击似乎有效,但尽管使用代码明显更改和填充框,但可见和背景之间存在差距.

From above, it appears my save click works, but although the boxes are visibly changed and filled out using the code, there is a gap between the visible and the background.

一些 HTML 源代码.Chrome 在检查我正在更改的元素时向我显示的内容:

Some HTML source code. Is what Chrome shows me when Inspecting an element I am changing:

    <fieldset>
        <legend>Proposal</legend>

        <div class="col-xs-12 col-sm-8 col-md-6">
            <div class="row">
                <div class="col-xs-2 form-group">
                    <label for="ProjectFile_ProposalLot">Lot</label><input class="form-control" data-bind="textInput: ProjectFile().ProposalLot" maxlength="100" name="ProjectFile-ProposalLot" type="text" />
                </div>
                    <div class="col-xs-2 form-group" data-bind="visible: ProjectFile().StateId() != 7 && ProjectFile().StateId() != 5">
                        <label data-bind="text: ProjectFile().ProposalDpLabel()"></label>
                        <input class="form-control" data-bind="textInput: ProjectFile().ProposalDp" maxlength="100" name="ProjectFile-ProposalDp" type="text" />
                        </div>

<div class="col-xs-2 form-group";data-bind="visible: ProjectFile().StateId() != 7 &&ProjectFile().StateId() != 5"><label data-bind="text: ProjectFile().ProposalDpLabel()"></label><输入类=表单控件"data-bind="textInput: ProjectFile().ProposalDp";最大长度=100"name="ProjectFile-ProposalDp";类型=文本"/>

I searched the source code for the page. I believe this might be important, but I am not a HTML coder. I have shortened it a bit

我搜索了页面的源代码.我相信这可能很重要,但我不是 HTML 编码员.我把它缩短了一点

There is also this:

Buildaform.model=new Buildaform.ProjectPageViewModel({ ... ,"ProposalLot":null .... }

还有这个:

I cannot release the website address or source code publicly.

我认为最后一个与它有关.不知道能不能改.

解决方案

我不能公开网站地址或源代码.

Per your comment that:

推荐答案

根据您的评论:

  1. 暂停我的代码,手动更改复选框并运行代码以保存(确实保存手动更改,但不保存编码的

问题似乎出在代码设置表单控件上,而不是出在点击保存按钮的代码上.

This seems to be a problem not related to VBA but with the behaviour of knockout - see this SO post. The pertinent comment is:

这似乎是一个与 VBA 无关的问题,但与 knockout 的行为有关 - 请参阅 此 SO 帖子.相关评论是:

Your problem is that ko subscribes on the click event inside the checked binding:

您的问题是 ko 订阅了已检查绑定内的 click 事件:

The questioner in that post is having a similar problem to you - they are trying to check a checkbox (to change the view) but it is not updating either the viewmodel, or the underlying model itself. Knockout is a MVVM framework.

该帖子中的提问者遇到了与您类似的问题 - 他们试图检查一个复选框(以更改视图),但它既没有更新视图模型,也没有更新底层模型本身.Knockout 是一个 MVVM 框架.>

您的问题中的赠品是您的手动更改提交,因为您在浏览器中通过点击执行操作时执行点击和更改,但您的编程方法仅对表单进行更改控制,但不是先点击.

The give-away in your question is that your manual changes commit because you perform a click-and-change when performing the action via point-and-click in the browser, but your programmatic method only does the change to the form control, but not the click first.

那么,如何通过 IE 通过 VBA 自动化来解决这个问题?

So, how to solve this via VBA automation through IE?

基于我上面引用的帖子中的解决方案,加上方法 此处 我会冒用下面的代码作为可能的解决方案,但请注意它未经测试...

Based on the solution in the post I referenced above, plus the method here I will hazard the code below as a possible solution, but please note it is untested ...

基本上,您需要单击"要更改的表单元素 - 然后更新控件值.希望点击"位意味着淘汰视图模型会根据更改"进行更新,然后模型数据将写入数据库(或其他):

Basically you need to 'click' on the form element you want to change - and then update the control value. Hopefully the 'clicking' bit will mean that the knockout viewmodel updates per the 'change', and from there, the model data will be written to the database (or whatever):

您的复选框示例:

If InStr(ExtractedName, "NewPermit") > 0 Then
// hopefully this will get knockout to apply the required binding before your change the value
objElement.Click
objElement.Checked = True

您的下拉示例:

Set DropDown = objHTML.getElementById("ProjectFile-AccreditedCertifierId")
// hopefully this will get knockout to apply the required binding before your change the value
Dropdown.Click
DropDown.selectedIndex = 1

希望有所帮助 - 完全是 3 管问题!祝你好运.

Hope that helps - quite the 3-pipe problem! Good luck.

这篇关于使用 Knockout 控制网页:未保存可见更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆