excel - 使用 VBA 从 excel 复制/粘贴到 IE 网页时如何使提交按钮出现?
问题描述
所以,这就是问题所在。我的代码设置基本上是为了将我从 excel 中获得的一些数据复制/粘贴到网站的文本框中。但是,我现在只想按提交按钮编写代码。但是,除非我手动将某些内容写入文本框中或手动粘贴某些内容,否则提交按钮会保持灰色。我的代码实际上是在文本框中输入了数据,但提交按钮仍然保持灰色,并且所以我不能点击它。
这是在文本框中写入内容之前和之后的 HTML 代码,导致提交按钮可点击或不可点击。
<div class="form-group has-error">
<div class="form-group">
这是该按钮的完整 html 代码:
<button disabled="" class="pull-right report-module-query-list-execute-button btn btn-sm btn-default" type="button"><span>Print</span></button>
这部分 html 代码用于文本框,但为了启用按钮而更改的是此代码。
<div class="form-group has-error">
<label class="col-sm-4 col-sm-offset-1 control-label" for="TaskStateHistoryPivot_1"><span>Search data in</span></label>
<div class="col-sm-7">
<div class="col-sm-12">
<textarea class="report-module-query-execution-freeform-area" rows="5"></textarea></div></div></div>
有任何想法吗?
解决方案
从您的代码和描述中,我想您正在验证 TextBox,然后将 CSS 样式附加到 div 或提交按钮,并使用它来启用/禁用提交按钮。
由于我没有找到使用 VBA 直接更改 CSS 样式或禁用属性的方法。
作为一种解决方法,我建议您可以在您的网站中添加一个隐藏按钮,然后,在其单击事件中,您可以检查文本框是否为空并启用/禁用提交按钮。设置文本框的值后,您可以触发此按钮并验证文本框的值并启用/禁用提交按钮。
网页代码如下:
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>
$(function () {
$("#btnSubmit").prop("disabled", true);
function validate() {
if ($("#Text1").val() != '') {
$("#btnSubmit").prop("disabled", false);
}
else {
$("#btnSubmit").prop("disabled", true);
}
}
$("#Text1").change(function () {
validate();
});
$("#btnvalidte").click(function () {
validate();
});
});
</script>
<div>
<input id="Text1" type="text" value="" />
<input id="btnvalidte" type="button" value="validate" style="display:none" />
<input id="btnSubmit" type="button" value="Submit" />
</div>
VBA 代码如下:
Sub Test()
Dim ie As Object
Dim Rank As Object
Set ie = CreateObject("InternetExplorer.application")
ie.Visible = True
ie.Navigate ("http://localhost:54382/HtmlPage47.html")
Do
If ie.ReadyState = 4 Then
Exit Do
Else
End If
Loop
Set doc = ie.document
'get the excel data
Dim val As String
val = Range("C2").Value
'find the textbox and set the value
doc.getElementById("Text1").Value = val
'trigger the validate button to check whether the textbox is empty and enable the submit button
doc.getElementById("btnvalidte").Click
End Sub
截图是这样的。
编辑:
您可以尝试使用此方法:使用 SendKeys 方法将值输入到文本框中,然后按 Tab 按钮触发验证规则。
代码如下:
Sub Test()
Dim ie As Object
Dim Rank As Object
Set ie = CreateObject("InternetExplorer.application")
ie.Visible = True
ie.Navigate ("http://localhost:54382/HtmlPage47.html")
Do
If ie.readyState = 4 Then
Exit Do
Else
End If
Loop
Set doc = ie.document
'get the excel data
Dim val As String
val = Range("C2").Value
'find the textbox and set the value
Set TextBoxfield = doc.getElementById("Text1")
'set focus on the textbox.
With TextBoxfield
.Focus
End With
'enter value into the textbox.
SendKeys CStr(val)
'press the Tab button to change the focus and try to trigger the validation rule
SendKeys "{TAB}"
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
End Sub
推荐阅读
- python - 如何自动修复 pylint 报告的 lint 问题?
- java - 在 pom.xml 中升级我的 spring 版本时显示异常
- html - 如何使 ng2-charts 垂直条形图水平滚动?
- sql - Oracle 中的 CTE 在没有选择语句的情况下无法工作
- design-patterns - Lua - 如果在 2 个特殊字符之间,使用 string.find 返回 true
- amazon-web-services - 在ec2 userdata cloudformation中使用for循环和其他声明的变量
- javascript - 递归:从数组javascript中获取对象
- python - 为什么会触发我的“If”语句?
- r - 使用动态 downloadHandler() 从闪亮的应用程序下载动态绘图图
- mariadb - 我可以从 MariaDB 收到有关 DDL 命令执行的通知吗