简体   繁体   English

如何使用Web浏览器控件C#调用jQuery函数

[英]How to call jQuery function using web browsers control C#

I want to call a jQuery event from C# via web browser control. 我想通过Web浏览器控件从C#调用jQuery事件。

Here's the jQuery function: 这是jQuery函数:

$("#veh-brand").change(function() {
    var brand = $(this).val();
    var dataString = "brand=" + brand;
    $text = $("#veh-brand [value='" + brand + "']").text();
    $('#full-car').html($text);
    $.ajax({
        type: "POST",
        url: "/external/ws-brand_year.php",
        data: dataString,
        cache: false,
        dataType: 'JSON',
        success: function(response) {
            var html = "<option value=''>Escoger</option>";
            for (var i = 0; i < response.length; i++) {
                html += "<option class='contrast' value=" + response[i].id + ">" + response[i].description + "</option>"
            }
            $("#veh-brand-year").html(html);
        }
    });
});

this.webBrowser1.Document.InvokeScript("change");

Can you tell me where I am wrong? 你能告诉我我哪里错了吗?

Using invoke you can call just methods like this: 使用invoke可以只调用如下方法:

function change(){
}

Don't put in document ready trigger. 不要放入文档就绪触发器。 Method must be located on accessible level. 方法必须位于可访问级别。

Just write code which will call needed method in change() and call it. 只需编写代码即可在change()中调用所需的方法并调用它。

If you use invokescript you should put inside JS code, not method name. 如果使用invokescript,则应放入JS代码,而不是方法名。

  dynamic tabclick = webBrowser1.Document.InvokeScript("eval", new[] { 
    "(function() {jQuery('.class').trigger('click');})()" });

I have used this in c# and it worked . 我已经在c#中使用了它,并且有效。 we can write the jquery coding in that function. 我们可以在该函数中编写jquery编码。

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

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