<script type="text/javascript">
var questionType = 'RQ_CHOICE';
var elementName="meetingQuestionAnswer(220674)";
if(questionType=="RQ_CHOICE" || questionType=="RQ_YES_NO" ){
$("select[name='" + elementName + "']").bind("change",function(event){
loadDependentQuestion("220674",'select');
});
}
if(questionType=="RQ_CHOICE_BULLET" || questionType=="RQ_RATING_SCALE"){
$("input:radio[name='" + elementName + "']").bind("change",function(event){
loadDependentQuestion("220674",'radio');
});
}
bindDelegateToloadDependentQuestion("220674");
</script>
在解决方案的帮助下,我解决了从JavaScript问题“ onchange”触发前一个问题答案的依赖dropboz的问题(请参阅有关此问题的链接),但是现在JavaScript代码的运行速度似乎比宏慢因此,当我需要使用Excel中的数据回答下一个问题时,宏会停止并显示一条错误消息,指出该对象不存在(因为新下拉列表(我的对象)尚未出现)。如果我进行调试,然后手动逐行进行操作,则可以花时间让JavaScript加载新的下拉菜单,一切正常。
onchange event in JavaScript - dependent option list
在触发事件以及带有do事件的循环之后,我尝试使用VBA中的wait函数,但是仍然无法正常工作。
我还制作了一个do循环来重复执行该操作,而该对象什么也不是,但也不起作用。
这是与三个问题相关的脚本:
'First question
ieDoc.getElementsByName("question1")(0).Value = "Yes"
event_onChange.initEvent "change", True, False
ieDoc.getElementsByName("question1")(0).dispatchEvent event_onChange
While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Wend
application.wait "00:00:02"
'Second question - the one that is not loading
Do while ieDoc.getElementsByName("question2")(0) Is Nothing Then
For i = 1 To totalRng.Cells.count
If totalRng.Cells(i).Offset(, savingsRng.Column - Cells(i).Column) > 0 Then
On Error Resume Next
While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Wend
ieDoc.getElementsByName("question2")(0).Value = "Yes"
Exit For
Else
While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Wend
ieDoc.getElementsByName("question2")(0).Value = "No"
End If
Next i
Loop
答案 0 :(得分:1)
看起来您想要一个循环直到出现,并根据超时进行循环以避免潜在的无限循环。
Const MAX_WAIT_SEC As Long = 10
Dim t As Date, ele As Object
t = Timer
Do
On Error Resume Next
Set ele = ieDoc.querySelector("[name='meetingQuestionAnswer(221010)']")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ieDoc.querySelectorAll("[name='meetingQuestionAnswer(221010)']").Length = 0
If Not ele Is Nothing Then
'do something
End If