怎样从html代码中提取出控件
比如: <p> <input type= "checkbox " checked= "checked " name= "name " value= "hhh " /> fasfdsafsdaf <input maxlength= "50 " size= "4 " name= "name " /> </p> ,中提出 " <input type= "checkbox " checked= "checked " name= "name " value= "hhh " /> "这样的控件,并用1等数字来替换,控件可能是其他类型的,谢谢指教!最好是C#的,javascript也行.
[解决办法]
用下面的代码试下
string yourStr = richTextBox1.Text;
MatchCollection mc = Regex.Matches(yourStr, @ " <(input|select)\s[^> ]*?> ([\s\S*]*? </\1> )? ", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups[0].Value + "\n\n\n "; //提取结果
}
别的方法我不会,只会用正则,只要楼主的需求明确,应该还是可以满足楼主的要求的,只不过可能并不是最佳的处理方式吧
[解决办法]
<body>
<div id= "s "> </div>
<form>
<input type= "text " name= "s1 "/>
<input type= "text " name= "s1 "/>
<input type= "text " name= "s1 "/>
<input type= "text " name= "s1 "/>
<input type= "text " name= "s1 "/>
<select name= "s "/>
</form>
<script>
k(document.body);
function k(obj){
for (var i = 0; i < obj.childNodes.length; i++){
if (obj.childNodes[i].hasChildNodes()){
k(obj.childNodes[i]);
}
var tn = obj.childNodes[i].nodeName.toLowerCase();
if (tn == "input " || tn == "select " || tn == "textarea "){
document.getElementById( "s ").innerHTML += tn + ": name: " + obj.childNodes[i].getAttribute( "name ") + " <br /> "
}
}
}
</script>
</body>
[解决办法]
来晚了
********************
html源——
<tr> <td> <input id= "MobileTypesImg_3 " type= "checkbox " name= "MobileTypesImg:3 "> <label for= "MobileTypesImg_3 "> kyocera-KZ-820 </label> </td>
<td> <input id= "MobileTypesImg_10 " type= "checkbox " name= "MobileTypesImg:10 "> <label for= "MobileTypesImg_10 "> LGE CU-8080 </label> </td>
<td> <input id= "MobileTypesImg_17 " type= "checkbox " name= "MobileTypesImg:17 " checked> <label for= "MobileTypesImg_17 "> MOT-V860 </label> </td>
<td> <input id= "MobileTypesImg_24 " type= "checkbox " name= "MobileTypesImg:24 "> <label for= "MobileTypesImg_24 "> SEC-schx319 </label> </td>
</tr>
<tr> <td> <input id= "MobileTypesImg_4 " type= "checkbox " name= "MobileTypesImg:4 "> <label for= "MobileTypesImg_4 "> kyocera-KZ-850 </label> </td>
<td> <input id= "MobileTypesImg_11 " type= "checkbox " name= "MobileTypesImg:11 "> <label for= "MobileTypesImg_11 "> LGE CU-8180 </label> </td>
<td> <input id= "MobileTypesImg_18 " type= "checkbox " name= "MobileTypesImg:18 " checked> <label for= "MobileTypesImg_18 "> MOT-V868 </label> </td>
<td> <input id= "MobileTypesImg_25 " type= "checkbox " name= "MobileTypesImg:25 "> <label for= "MobileTypesImg_25 "> SEC-schx339 </label> </td>
</tr>
/***
把数据库查询结果绑定到一个checkboxlist,然后要根据里面各个checkbox不同的值来设置各个checkbox的状态是可选中或不可选中(disabled=true)!
***/
<script language= "JavaScript ">
var disUAs = document.all.DisabledUAs.value;//要屏蔽的checkbox 的对应label里的文本
for(var i=0;i <document.all.length;i++)
{
var ctrl=document.all[i];
if(ctrl.id!=null)
{
//is element label
if((ctrl.htmlFor!=null)&&(ctrl.htmlFor!= "undefined ")&&(ctrl.htmlFor!= " "))
{
var lbl=ctrl;
//find checkbox
var chk=document.getElementById(lbl.htmlFor);
if(chk==null)
break;
}
else
{
var reg=new RegExp(lbl.innerText, "g ");
if(disUAs.match(reg)!=null)// or use if(disUAs.search(reg)!=-1)
chk.disabled= "disabled ";
}
}
}//end of for
</script>
[解决办法]
C# 的——
*****************************
/////从当前网面中获取所有的控件的输入值包括:TextBox,DropDownList、CheckBox、ListBox、DataList
/////参数:WebCurPage 要履历的网页
/////参数:strArrCtrlId 要遍历的Web页中控件ID数组
/////参数:nOpeType 操作类型1、GetDataFrom WebCtrl 's value ;2、Celar WebCtrl 's Value
public String[] CeGetWebData(System.Web.UI.Page WebCurPage,String[] strArrCtrlId,int nOpeType)
{
String[] strArrEmpty=new String[1];
if(nOpeType!=1 && nOpeType!=2)
{
strArrEmpty[0]= "参数 nOpeType只能为数据1、2 ";
return strArrEmpty;
}
int nWebCtrls=strArrCtrlId.Length;
strArrData=new String[nWebCtrls];
String strCtrlType= " ";
TextBox CtrlTextComId;
DropDownList CtrlDrpDwnListId;
ListBox CtrlListBox;
for(int nCtrls=0;nCtrls <nWebCtrls;nCtrls++)
{
strCtrlType=WebCurPage.FindControl(strArrCtrlId[nCtrls]).GetType().ToString();
if(strCtrlType== "System.Web.UI.WebControls.TextBox ")
{
CtrlTextComId=(TextBox)WebCurPage.FindControl(strArrCtrlId[nCtrls]);
if(nOpeType==1)
{
strArrData[nCtrls]=CtrlTextComId.Text;
}
else if(nOpeType==2)
{
CtrlTextComId.Text= " ";
}
else if(nOpeType==3)
{
CtrlTextComId.Text=strArrData[nCtrls];
}
}
else if(strCtrlType== "System.Web.UI.WebControls.DropDownList ")
{
CtrlDrpDwnListId=(DropDownList)WebCurPage.FindControl(strArrCtrlId[nCtrls]);
if(nOpeType==1)
{
if(CtrlDrpDwnListId.SelectedIndex!=-1 && CtrlDrpDwnListId.Items.Count> 0)
strArrData[nCtrls]=CtrlDrpDwnListId.SelectedItem.Text;
}
else if(nOpeType==2)
{
CtrlDrpDwnListId.SelectedIndex=-1;
if(CtrlDrpDwnListId.Items.Count> 0)
CtrlDrpDwnListId.Items.Clear();
}
else if(nOpeType==3)
{
CtrlDrpDwnListId.Items.Add(strArrData[nCtrls]);
}
}
else if(strCtrlType== "System.Web.UI.WebControls.DataList ")
{
}
else if(strCtrlType== "System.Web.UI.WebControls.ListBox ")
{
CtrlListBox=(ListBox)WebCurPage.FindControl(strArrCtrlId[nCtrls]);
if(nOpeType==1)
{
if(CtrlListBox.SelectedIndex!=-1 && CtrlListBox.Items.Count> 0)
strArrData[nCtrls]=CtrlListBox.SelectedItem.Text;
}
else if(nOpeType==2)
{
CtrlListBox.SelectedIndex=-1;
if(CtrlListBox.Items.Count> 0)
CtrlListBox.Items.Clear();
}
else if(nOpeType==3)
{
CtrlListBox.Items.Add(strArrData[nCtrls]);
}
}
else if(strCtrlType== "System.Web.UI.WebControls.CheckBox ")
{
}
}
return this.strArrData;
}//end