如何在VB6中使用execScript检索表单值? [英] How to retrive form value using execScript in VB6?

查看:27
本文介绍了如何在VB6中使用execScript检索表单值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,这是我的代码

将地址变暗为字符串

WebBrowser1.Document.parentWindow.execScript("var a =document.form1.address.text"、JavaScript")

WebBrowser1.Document.parentWindow.execScript("var a = document.form1.address.text", "JavaScript")

如何将 document.form1.address.text 的值提取到我的 VB6 变量 Address 中?

how can i extract the value of document.form1.address.text to my VB6 variable Address?

推荐答案

您可以使用 DOM.

假设我们有一个简单的 HTML 表单:

Let us say we have simple HTML form:

<html>
<body>
    <form name="form1">
        Address: <input type="text" id="address">
    </form> 
</body>
</html>

WebBrowser控件中加载并确保DOM准备就绪后,我们可以通过以下方式获取address字段的文本:

After loading it in a WebBrowser control and making sure DOM is ready, we can get text of address field in the following way:

Private Sub cmdGetAddressText_Click()
    Dim HTMLElement As Object
    Dim Address As String

    Set HTMLElement = WebBrowser1.Document.GetElementByID("address")
    Address = HTMLElement.Value

    MsgBox Address
End Sub

它甚至比这更简单.您可以直接从 VB6 访问字段值:

It's even simpler than that. You can access field value directly from VB6:

Address = WebBrowser1.Document.Form1.Address.Value

编辑#2

如果您愿意,也可以获取 JavaScript 变量的值:

It is also possible to get a value of a JavaScript variable if you wish to do so:

Private Sub cmdJSVar_Click()
    Dim Address As String
    Call WebBrowser1.Document.parentWindow.execScript("var a=document.form1.address.value; alert(a);")
    Address = WebBrowser1.Document.Script.a

    MsgBox Address
End Sub

请注意,.Script.a 中的 JS 变量名称区分大小写(即 .Script.A 不起作用).花了一些时间才弄明白.

Notice that JS variable name in .Script.a is case-sensitive (i.e. .Script.A won't work). It took some time to figure this out.

这篇关于如何在VB6中使用execScript检索表单值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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