js动态添加表格

JavaScript058

js动态添加表格,第1张

<script type="text/javascript">

var $ = function(id){

return document.getElementById(id)

}

//全选

function checkAll(target) {

var checkeds = document.getElementsByName("b_id")

for (var i = 0i <checkeds.lengthi++) {

checkeds[i].checked = target.checked

}

}

//刷新行号

function refreshRowNo() {

var tbody = $("tbody")

for (var i = 0i <tbody.rows.lengthi++) {

tbody.rows[i].cells[0].innerHTML = i + 1

}

}

//添加行

function AddRow() {

var tbody = $("tbody")

var row = tbody.insertRow(tbody.rows.length)

row.insertCell(row.cells.length)

row.insertCell(row.cells.length).innerHTML = '<input type="checkbox" name="b_id" />'

row.insertCell(row.cells.length).innerHTML = '<input type="text" name="name" />'

row.insertCell(row.cells.length).innerHTML = '<input type="text" name="number" />'

row.insertCell(row.cells.length).innerHTML = '<input type="text" name="price" />'

row.insertCell(row.cells.length).innerHTML = '<input type="text" name="sum" />'

refreshRowNo()

}

//删除行

function DelRow() {

var checkeds = document.getElementsByName("b_id")

var ischeck = false

for (var i = checkeds.length - 1i >= 0i--) {

if (checkeds[i].checked) {

ischeck = true

break

}

}

if (ischeck) {

if (confirm("确定删除选中行?")) {

for (var i = checkeds.length - 1i >= 0i--) {

if (checkeds[i].checked) {

var index = checkeds[i].parentNode.parentNode.rowIndex

$("tbody").deleteRow(index - 1)

}

}

refreshRowNo()

}

} else {

alert("请选中需要删除的行!")

}

}

//保存

function Save() {

var detail = [],

tbody = $("tbody")

for (var i = 0i <tbody.rows.lengthi++) {

var name = tbody.rows[i].cells[2].childNodes[0].value

var number = tbody.rows[i].cells[3].childNodes[0].value

var price = tbody.rows[i].cells[4].childNodes[0].value

var sum = tbody.rows[i].cells[5].childNodes[0].value

var item = name + "^" + number + "^" + price + "^" + sum

detail.push(item)

}

var detailstr = detail.join("|")

$("detail").value = detailstr

$("myform").submit()

}

</script>

<form name="myform" action="AddOrder.aspx" method="post">

<input type="hidden" id="detail" name="detail" />

<ul>

<li>采购单编号:<input type="text" id="CGDBH" name="CGDBH" /></li>

<li>

供应商名称:<input type="text" id="GYSMC" name="GYSMC" />

<input type="button" onclick="AddRow()" value="增加一行" />

<input type="button" onclick="DelRow()" value="删除" />

<input type="button" onclick="Save()" value="保存" />

</li>

</ul>

<table cellpadding="1" cellspacing="2">

<tr>

<th style="width:60px">序号</th>

<th style="width:20px"><input type="checkbox" onclick="checkAll(this)" /></th>

<th style="width:120px">名称</th>

<th style="width:120px">数量</th>

<th style="width:120px">单价</th>

<th style="width:120px">总金额</th>

</tr>

<tbody id="tbody">

<tr>

<td>1</td>

<td><input type="checkbox" name="b_id" /></td>

<td><input type="text" name="name" /></td>

<td><input type="text" name="number" /></td>

<td><input type="text" name="price" /></td>

<td><input type="text" name="sum" /></td>

</tr>

</tbody>

</table>

</form>

1,动态添加验证规则

// 添加

$("#addConnectUser").rules("add",{rules:{required:true,isString:true},messages:{required:"用户名为必填项",isString:"请输入规范字符"}})

// 移除$("#addConnectUser").rules('remove','required')

在此我要讲一下为什么有一个isString验证规则;这个是为了限制表单中input的输入规则;比如:不允许特殊字符,或者一些特殊的要求;这个isString是自己配置的

jQuery.validator.addMethod("isString", function(value, element) {

                return this.optional(element) || (inputTest.test(value))

            },"请输入规范内容")

// 此处的inputTest为你自定义的验证规则

2,如何给已经添加了表单验证的选项移除验证规则

$("#addConnectUser").rules("remove",'required')

//再次添加可以直接用$("#addConnectUser").rules("add",'required')

下面是用js动态创建form然后进行post提交的方式,参数url是表单提交的去向,参数params是给去向传递的参数(JSON结构),参数target设置何处打开链接文档(可选):

function postcall( url, params, target){

var tempform = document.createElement("form")

tempform.action = url

tempform.method = "post"

tempform.style.display="none"

if(target) {

tempform.target = target

}

for (var x in params) {

var opt = document.createElement("input")

opt.name = x

opt.value = params[x]

tempform.appendChild(opt)

}

var opt = document.createElement("input")

opt.type = "submit"

tempform.appendChild(opt)

document.body.appendChild(tempform)

tempform.submit()

document.body.removeChild(tempform)

}