【问题标题】:How to set a checkbox to "checked" using mshtml?如何使用 mshtml 将复选框设置为“选中”?
【发布时间】:2010-10-23 06:55:30
【问题描述】:

我可以用 mshtml 做大部分我需要做的事情,但我有点不知道如何将复选框输入元素设置为“已选中”。情况是这样的……

IHTMLElementCollection inputElements = (IHTMLElementCollection)doc.all.tags("input");
foreach (IHTMLElement el in inputElements)
{
    string elementHtml = el.outerHTML;
    string termsOfServiceIdentifier = "id=chkUTOS_ver2";

    //  select the Terms of Service checkbox
    if (elementHtml.Contains(termsOfServiceIdentifier)) 
    {
        HTMLInputElement chkTOS = (HTMLInputElement)el;
        chkTOS.@checked = true;  //  that's the solution. Thanks Wayne.
     }
     else
     {
        //  do nothing - we're not interested in this element
     }
}

提前感谢您的帮助!

格雷格

【问题讨论】:

    标签: c# .net html webforms mshtml


    【解决方案1】:

    HTMLInputElement 将 Checked 属性公开为布尔值

    【讨论】:

    • 我之前一定搞砸了,因为它不在 Intellisense 中。现在肯定是。韦恩干杯!
    • 我刚刚尝试过,发现我必须使用 C# 中的 @checked 才能编译它。 checked 是保留字。
    【解决方案2】:

    在纯 JavaScript 中,复选框元素具有 checked 属性。所以[用纯 JavaScript] 你可以这样写:

    document.getElementById("myCheckbox").checked = true;
    

    我不知道 .NET 或您在那里使用的任何东西,但他们可能会以类似的方式进行操作。

    史蒂夫

    【讨论】:

    • 感谢您的帮助。顺便说一句,这是 C#。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    相关资源
    最近更新 更多