<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function submitInfo(){
//顾客姓名张三,年龄40,有2个孩子,在规划局工作,住在南京
//下面这个数组,根据需要拼接的内容定义,如果需要将填写内容插入,则对应数字填插入位置下标,
//反之只需要填一个大于下标的数字即可
var arr = [["顾客姓名",4],["年龄",2],["有个孩子",1],["在工作",1],["住在",2]];
var retStr = "";
$(".info").each(function(i){
retStr += arr[i][0].slice(0,arr[i][1])+this.value+arr[i][0].slice(arr[i][1])+",";
});
//alert(retStr.substring(0,retStr.length-1));
return retStr.substring(0,retStr.length-1);
}
</script>
</head>
<body>
<label for="tx_name">姓名</label><input type="text" class="info" id="tx_name" />
<label for="tx_age">年龄</label><input type="text" class="info" id="tx_age" />
<label for="tx_child">孩子</label><input type="text" class="info" id="tx_child" />
<label for="txt_work">工作</label><input type="text" class="info" id="txt_work" />
<label for="txt_adress">住址</label><input type="text" class="info" id="txt_adress" />
<button onclick="submitInfo();">提交</button>
</body>
</html>
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function submitInfo(){
//顾客姓名张三,年龄40,有2个孩子,在规划局工作,住在南京
//下面这个数组,根据需要拼接的内容定义,如果需要将填写内容插入,则对应数字填插入位置下标,
//反之只需要填一个大于下标的数字即可
var arr = [["顾客姓名",4],["年龄",2],["有个孩子",1],["在工作",1],["住在",2]];
var retStr = "";
$(".info").each(function(i){
retStr += arr[i][0].slice(0,arr[i][1])+this.value+arr[i][0].slice(arr[i][1])+",";
});
//alert(retStr.substring(0,retStr.length-1));
return retStr.substring(0,retStr.length-1);
}
</script>
</head>
<body>
<label for="tx_name">姓名</label><input type="text" class="info" id="tx_name" />
<label for="tx_age">年龄</label><input type="text" class="info" id="tx_age" />
<label for="tx_child">孩子</label><input type="text" class="info" id="tx_child" />
<label for="txt_work">工作</label><input type="text" class="info" id="txt_work" />
<label for="txt_adress">住址</label><input type="text" class="info" id="txt_adress" />
<button onclick="submitInfo();">提交</button>
</body>
</html>
html:
<div id="info">
<label name="needed">顾客</label><input name="needed" type="text"></input>
<label name="needed">,有</label><input name="needed" type="text"></input>
<label name="needed">个孩子,在</label><input name="needed" type="text"></input>
<label name="needed">工作。</label>
</div>
<input type="button" id="getinfo" value="提取信息"></input>
将你需要提取的信息用label括起来,并将其name设置为needed。
js:
window.onload = function()
{
var btn_getinfo = document.getElementById("getinfo");
btn_getinfo.onclick = function()
{
var info = "";
var needed = document.getElementsByName("needed");
var length = needed.length;
for(var i=0; i<length; i++)
{
info += needed[i].value || needed[i].innerHTML; //value用于提取input的信息,innerHTML用于提取label的内容
}
alert(info);
};
}
<script>
$(function() {
$('#bt').click(function() {
s = $('form').html();
$('input').each(function() {
s = s.replace(/<input.+?>/i, $(this).val());
});
alert(s.replace(/<.+?>/g, ''));
});
});
</script>
<form>
姓名<input type="text" class="info" id="tx_name" />
年龄<input type="text" class="info" id="tx_age" />
孩子<input type="text" class="info" id="tx_child" />
工作<input type="text" class="info" id="txt_work" />
住址<input type="text" class="info" id="txt_adress" />
<input type=button id=bt value=ok>
</form>
如果不影响你的代码的话,将label标签都添加一个属性id="needed",如<label name="needed" id="needed">顾客</label>,操蛋的IE、、、
如果是个配置文件成百上千行,定义name也很崩溃的