C# 如果选中复选框,如何获取
我正在尝试获取复选框的checked属性。我尝试使用C# 如果选中复选框,如何获取,c#,gecko,geckofx,C#,Gecko,Geckofx,我正在尝试获取复选框的checked属性。我尝试使用geckohtmlement的 GetAttribute()函数,但它总是为选中的属性返回空值 我试试这个: Gecko.GeckoElementCollection z = Browser.Document.GetElementsByName("productBox[]"); foreach (Gecko.GeckoNode o in z) { if (o.NodeType == NodeType.Element) {
geckohtmlement
的GetAttribute()
函数,但它总是为选中的属性返回空值
我试试这个:
Gecko.GeckoElementCollection z = Browser.Document.GetElementsByName("productBox[]");
foreach (Gecko.GeckoNode o in z)
{
if (o.NodeType == NodeType.Element)
{
GeckoHtmlElement asd = (GeckoHtmlElement)o;
string aa =asd.GetAttribute("checked");
if (aa != null && aa == "True")
liaaa.Add(o);
}
}
复选框的Html:
<input name="productBox[]" value="10914" class="noborder" type="checkbox">
更新:
我完成了我的工作,但我的方式丑陋、肮脏且不易使用。在浏览器DomMouseUp
event:
GeckoElement active = (GeckoElement)browser.Document.ActiveElement;
if (active.Attributes["name"].TextContent == "productBox[]")
{
if (getHtmlelement(active).Parent.GetAttribute("style") == null ||
getHtmlelement(active).GetAttribute("style") == "")
{
getHtmlelement(active).SetAttribute("style", "MyCheck");
liaaa.Add(active);
}
else
{
getHtmlelement(active).SetAttribute("style", "");
liaaa.Remove(active);
}
}
它现在可以工作了,因为当页面加载时,默认情况下不会选中所有复选框。但是一个更好的方法会有很大帮助
感谢对GeckoInputElement进行强制转换将使您能够访问选中的属性:
if (o.NodeType == NodeType.Element && o is GeckoInputElement)
{
GeckoInputElement asd = (GeckoInputElement)o;
if (asd.Checked)
....
}
asd.Attributes[“checked”]
返回一个GeckoNode
而不是字符串,并且GeckoNode
为空。如果checkbox标记包含checked属性,那么会发生什么@Coder它返回“checked”,但只有当复选框在默认html中被选中时,并且如果我像asd.SetAttribute(“checked”,“checked”)
已选中,但仍GetAttribute
为checked
属性返回null。但是GetAttribute
在其他属性上工作正常。
string aa =asd.Attributes["checked"];
if (o.NodeType == NodeType.Element && o is GeckoInputElement)
{
GeckoInputElement asd = (GeckoInputElement)o;
if (asd.Checked)
....
}