<meta http-equiv="Content-Type" content="text/html charset=gb3212"/>
<head>
<style type="text/css">
div { margin: 4px auto text-align: center }
label { display: inline-block text-align: right width: 100px margin-right: 10px }
button { margin: 4px }
input { width: 150px }
</style>
<script>
//没用使用jquery, 如果是jquery,会更容易一些。
window.onload = function(){
var form = document.createElement("form")
form.setAttribute("method", "post")
form.append(createRow("username", "text", "用户名: "))
form.append(createRow("userpwd", "password", "密码: "))
form.append(createRow("reuserpwd", "password", "确认密码: "))
form.append(createRow("useremail", "text", "电子邮件: "))
var div = document.createElement("div")
var btn = document.createElement("button")
btn.setAttribute("type", "button")
btn.append(document.createTextNode("验证"))
btn.onclick = function(){ return checkform(false) }
div.append(btn)
btn = document.createElement("button")
btn.setAttribute("type", "submit")
btn.append(document.createTextNode("提交"))
btn.onclick = function(){ return checkform(true) }
div.append(btn)
form.append(div)
document.body.append(form)
}
function createRow(_name, _type, _text){
var div = document.createElement("div")
div.append(createLabel(_text))
div.append(createTextBox(_name, _type))
return div
}
function createTextBox(_name, _type){
var txt = document.createElement("input")
txt.setAttribute("type", _type)
txt.setAttribute("name", _name)
return txt
}
function createLabel(_text){
var lbl = document.createElement("label")
lbl.append(document.createTextNode(_text))
return lbl
}
function checkform(bln){
var form = document.forms[0]
if(form.username.value.length < 4){
alert("用户名至少包含4个字符")
return false
}
if(form.userpwd.value.length < 8){
alert("密码至少包含8个字符")
return false
}
if(form.userpwd.value != form.reuserpwd.value){
alert("两次密码不一致")
return false
}
if(!(/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/gi).test(form.useremail.value)){
alert("邮箱不符要求")
return false
}
if(!bln){ alert("验证通过") }
return bln
}
</script>
</head>
<body>
</body>
</html>
var a = document.createElement("form")var b = document.createElement("input")
var c = b
b.setAttribute("type","text")
c.setAttribute("type","submit")
b.setAttribute("name","1")
c.setAttribute("name","2")
document.body.appendChild(a)
a.appendChild(b)
a.appendChind(c)
提交是:
c.click()制造submit按钮的点击时间,自动完成确认
/*** @see 获得表单里的数据(不光是表单,任何元素都可以)
* @param id 表单的ID
* @param type 要返回的类型: string返回用&拼接的字符串, object返回一个js对象, json表示返回json格式的字符串
* @return 根据type而定,默认为string
*/
function getFormData(id, type) {
if (getO(id) != null) {
var str = ""
var obj = new Object()
var json = "{"
var array = getO(id).getElementsByTagName("input")
var rdbArr = new Array()
var ckbArr = new Array()
for (var i = 0i <array.lengthi++) {
if (array[i].name != null &&array[i].name != "") {
if (array[i].type == null || array[i].type == "") {
array[i].type = "text"
}
var inType = array[i].type.toLowerCase()
if (inType == "text" || inType == "hidden" || inType == "password") {
str += array[i].name + "=" + array[i].value + "&"
obj[array[i].name] = array[i].value
json += array[i].name + ":'" + array[i].value.replace(/\'/g, "\"") + "',"
} else if (inType == "radio") {
if (!isArrayHave(rdbArr, array[i].name)) {
rdbArr[rdbArr.length] = array[i].name
}
} else if (inType == "checkbox") {
if (!isArrayHave(ckbArr, array[i].name)) {
ckbArr[ckbArr.length] = array[i].name
}
}
}
}
for (var i = 0i <rdbArr.lengthi++) {
var key = rdbArr[i], value = getRadio(rdbArr[i])
str += key + "=" + value + "&"
obj[key] = value
json += key + ":'" + value.replace(/\'/g, "\"") + "',"
}
for (var i = 0i <ckbArr.lengthi++) {
var key = ckbArr[i], value = getBox(ckbArr[i])
str += key + "=" + value + "&"
obj[key] = value
json += key + ":'" + value.replace(/\'/g, "\"") + "',"
}
array = getO(id).getElementsByTagName("textarea")
for (var i = 0i <array.lengthi++) {
if (array[i].name != null &&array[i].name != "") {
str += array[i].name + "=" + array[i].value + "&"
obj[array[i].name] = array[i].value
json += array[i].name + ":'" + array[i].value.replace(/\'/g, "\"") + "',"
}
}
array = getO(id).getElementsByTagName("select")
for (var i = 0i <array.lengthi++) {
if (array[i].name != null &&array[i].name != "") {
str += array[i].name + "=" + array[i].value + "&"
obj[array[i].name] = array[i].value
json += array[i].name + ":'" + array[i].value.replace(/\'/g, "\"") + "',"
}
}
str = (str.length >0 ? str.substring(0, str.length - 1) : str)
json = (json.length >1 ? json.substring(0, json.length - 1) : json)
json += "}"
if (type == "object") {
return obj
} else if (type == "json") {
return json
} else {
return str
}
} else {
alertWin(id + "\u672a\u5b9a\u4e49")
}
return ""
}