今天用C#写一个登陆论坛发帖的程序,用webBrowser时出现了个小问题,请教下!页面的下拉框源码如下 <select id="cltype" onchange="szchange();"><option selected>请选择您的职业</option><option value="0">学生</option><option value="1">老师</option>另外一个下拉框是根据这里选择的职业动态生成的;默认的<select id="clall" onchange="allchange();"><option selected>请选择您的月薪</option>  是这样,选择了上面的以后,网页加入了<option value="1000">1000</option><option value="10000">10000</option>这样的,我用webBrowser SetAttribute 不起任何作用,  在页面CreateElement("script")  使用脚本来选择也无效,我确定我的代码没有问题!直接 document.getElementById('clall').add(new Option("1000","1000") 也无效,请教这里应该怎么处理才能设置这里的clall控件的选中值!

解决方案 »

  1.   

    <select id="clall"></select>
    <script type="text/javascript">
    var clall=document.getElementById("clall");
    var option=document.createElement("option");
    option.text = "1000元";
    option.id="option_1000";
    option.value="1000";
    clall.appendChild(option);
    //-------------
    option=document.createElement("option");
    option.text = "10010元";
    option.id="option_10010";
    option.value="10010";
    clall.appendChild(option);
    </script>
      

  2.   

    感谢楼上的回复,但是我的是在 webBrowser 下读取,不是javascript添加!
      

  3.   

      /// <summary>
            /// 选中select标签
            /// </summary>
            /// <param name="webbrowser"></param>
            /// <param name="controlName"></param>
            /// <param name="selectString"></param>
            public void GetSelect(System.Windows.Forms.WebBrowser webbrowser, string controlName, string selectString)
            {
                System.Windows.Forms.HtmlElementCollection collection = webbrowser.Document.All[controlName].Children;
                for (int i = 0; i < collection.Count; i++)
                {
                    if (collection[i].GetAttribute("innerText") == selectString)
                    {
                        webbrowser.Document.All[controlName].Children[i].SetAttribute("selected", "selected");
                        return;
                    }
                }
            }controlName是控件的id
    selectString是需要选中的那一列的值
      

  4.   

    <select id="clall"></select>
    <script type="text/javascript">
    var clall=document.getElementById("clall");
    var option=document.createElement("option");
    option.text = "1000元";
    option.id="option_1000";
    option.value="1000";
    clall.appendChild(option);
    //-------------
    option=document.createElement("option");
    option.text = "10010元";
    option.id="option_10010";
    option.value="10010";
    clall.appendChild(option);
    document.getElementById("option_10010").selected=true;//选中  option有id的情况下
    clall.selectedIndex=0;//选中 从0开始   在option没有id的情况下
    </script>
      

  5.   

    HtmlElement hElement;            
    hElement = WebBrowser.Document.GetElementById("selectId");
    hElement.SetAttribute("selectedIndex", "1");