简体   繁体   English

为什么我不能使用C#WebBrowser控件触发选择更改事件?

[英]Why can't I trigger select change event using C# WebBrowser control?

In the javascript I see the following defined: 在javascript中,我看到以下定义:

 $('#dropDownId').change(function(){        
 ... do stuff...          
 });

 <select id="dropDownId">
     <option value="1">value 1</option>
     <option value="2">value 2</option>
     <option value="3">value 3</option>
 </select>

I've tried using the following code: 我尝试使用以下代码:

 webBrowser.Document.GetElementById("dropDownId").SetAttribute("value", "1");
 webBrowser.Document.GetElementById("dropDownId").Children[1].SetAttribute("selected", "selected");
 webBrowser.Document.GetElementById("dropDownId").InvokeMember("onchange");

I can see the drop down get changed to the right value, but the following never gets executed: 我可以看到下拉菜单已更改为正确的值,但是以下操作从未执行:

 $('#dropDownId').change(function(){        
 ... do stuff...          
 });

Also, when I look at the properties for "dropDownId" in Chrome, the "onchange" event is null, so how can I invoke the above "change" script for the dropdown? 另外,当我在Chrome中查看“ dropDownId”的属性时,“ onchange”事件为null,那么如何为下拉菜单调用上述“ change”脚本?

jquery attaches events to allow for multiple event handlers, that's why onchange property of your select is null. jquery附加事件以允许多个事件处理程序,这就是为什么select onchange属性为null的原因。

You can do: 你可以做:

function myChange(){        
 ... do stuff...          
}

<select id="dropDownId" onchange="myChange">
...
</select>

or 要么

webBrowser.Document.InvokeScript("myChange");

or 要么

function myChange_Jquery(){
  $("#dropDownId").change();
}

webBrowser.Document.InvokeScript("myChange_Jquery");

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

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