如何获取html页面的所有控件的xml,并使用JavaScript将该xml中的数据填充到该页面的控件中 [英] How do I get xml of all controls of html page and fill data from that xml to controls of that page using JavaScript
问题描述
我有一个html页面...我想
1.获取该页面所有控件的XML
2.将生成的xml中的数据填充到所有控件那个页面。
我想用HTML和脚本语言来执行这个操作...我也想要一个generalize函数,它可以为任何表单创建XML或者将任何XML注入到表单的控件中。 />
例如....我的页面是...
I have a html page... I want to
1. get XML of all controls of that page
2. fill data from generated xml to all controls of that page.
I want to perform this operation using HTML and scripting language... also I want a generalize function which create XML for any form or inject any XML to controls of form..
For Example.... my page is...
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Name</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Gender</td>
<td>
<asp:RadioButtonList ID="rblGender" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td>Hobbies</td>
<td>
<asp:CheckBoxList ID="chkHobbies" runat="server">
<asp:ListItem>Facebook</asp:ListItem>
<asp:ListItem>Whats app</asp:ListItem>
<asp:ListItem>Skype</asp:ListItem>
<asp:ListItem>Chat Messanger</asp:ListItem>
<asp:ListItem>Twitter</asp:ListItem>
</asp:CheckBoxList>
</td>
</tr>
<tr>
<td>Exp.</td>
<td>
<asp:DropDownList ID="ddlExp" runat="server">
<asp:ListItem><1 year</asp:ListItem>
<asp:ListItem>1-2 year</asp:ListItem>
<asp:ListItem>2-5 year</asp:ListItem>
<asp:ListItem>5-10 year</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnBlankXML" runat="server" Text="Get Blank XML"
onclick="btnBlankXML_Click" />
</td>
<td><asp:Button ID="btnXMLWithData" runat="server" Text="XML with data"
onclick="btnXMLWithData_Click" /></td>
</tr>
</table>
label stRT
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
LABEL END
</div>
</form>
</body>
</html>
预期的XML应该是....
Expected XML should be....
<form1>
<txtName>Test Data</txtName>
<rblGender>0</rblGender>
<chkHobbies>1,3,5</chkHobbies>
<ddlExp>2</ddlExp>
<btnBlankXML />
<btnXMLWithData />
<Label1 />
</form1>
推荐答案
嘿,您可以使用以下函数生成xml for控制。在下面的代码中,我在新窗口中显示了xml。您可以根据需要修改代码以使用该xml。我没有在xml标签中添加值,因为你没有提到有关值的详细信息。
Hey you can use below function to Generate xml for controls. In the below code I have displayed xml in new window . You can modify the code to make use of that xml however you want. I haven't added values in the xml tags as in question you haven't mentioned about values in details.
function displayFormData() {
NewWindow = open("", "window2")
NewWindow.document.open("text/plain")
var i = 0;
var j = 0;
var x = "";
for (i = 0; i < document.forms.length; ++i) {
x = "<" + document.forms[i].id + ">";
for (j = 0; j < document.forms[i].elements.length; ++j) {
x = x + "<" + document.forms[i].elements[j].id + ">";
x = x + "</" + document.forms[i].elements[j].id + ">";
}
x = x + "</" + document.forms[i].id + ">";
NewWindow.document.write(x);
}
NewWindow.document.close()
return false
}
告诉我你是否需要任何进一步的帮助!!
希望这有助于: - )
Tell me if you need any further help on this one !!
Hope this helps :-)
这篇关于如何获取html页面的所有控件的xml,并使用JavaScript将该xml中的数据填充到该页面的控件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!