jQuery動(dòng)態(tài)操作表單示例【基于table表格】
本文實(shí)例講述了jQuery動(dòng)態(tài)操作表單。分享給大家供大家參考,具體如下:
<html>
<head>
<title>jquery表格操作</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<style type="text/css">
table
{
border: black solid 1px;
border-collapse: collapse;
}
td
{
border: black solid 1px;
padding: 3px;
}
.td_Num
{
width: 60px;
text-align: center;
}
.td_Item
{
width: 160px;
text-align: center;
}
.td_Oper
{
width: 120px;
text-align: center;
}
.td_Oper span
{
cursor: pointer;
}
</style>
</head>
<body>
<table>
<tr>
<td class='td_Num'>
序號
</td>
<td class='td_Item'>
步驟名稱
</td>
<td class='td_Item'>
步驟描述
</td>
<td class='td_Oper'>
相關(guān)操作 <a href="#" rel="external nofollow" onclick="add_line();">添加</a>
</td>
</tr>
</table>
<table id="content">
</table>
<input type="button" value="提交數(shù)據(jù)" id="btnSubmit" onclick="SaveData()" />
</body>
</html>
<script type="text/javascript">
var currentStep = 0;
var max_line_num = 0;
//添加新記錄
function add_line() {
max_line_num = $("#content tr:last-child").children("td").html();
if (max_line_num == null) {
max_line_num = 1;
}
else {
max_line_num = parseInt(max_line_num);
max_line_num += 1;
}
$('#content').append(
"<tr id='line" + max_line_num + "'>" +
"<td class='td_Num'>" + max_line_num + "</td>" +
"<td class='td_Item'><input type='text' class='stepName' value='步驟名稱" + max_line_num + "'></input></td>" +
"<td class='td_Item'><input type='text' class='stepDescription' value='步驟描述" + max_line_num + "'></td>" +
"<td class='td_Oper'>" +
"<span onclick='up_exchange_line(this);'>上移</span> " +
"<span onclick='down_exchange_line(this);'>下移</span> " +
"<span onclick='remove_line(this);'>刪除</span> " +
"</td>" +
"</tr>");
}
//刪除選擇記錄
function remove_line(index) {
if (index != null) {
currentStep = $(index).parent().parent().find("td:first-child").html();
}
if (currentStep == 0) {
alert('請選擇一項(xiàng)!');
return false;
}
if (confirm("確定要?jiǎng)h除改記錄嗎?")) {
$("#content tr").each(function () {
var seq = parseInt($(this).children("td").html());
if (seq == currentStep) { $(this).remove(); }
if (seq > currentStep) { $(this).children("td").each(function (i) { if (i == 0) $(this).html(seq - 1); }); }
});
}
}
//上移
function up_exchange_line(index) {
if (index != null) {
currentStep = $(index).parent().parent().find("td:first-child").html();
}
if (currentStep == 0) {
alert('請選擇一項(xiàng)!');
return false;
}
if (currentStep <= 1) {
alert('已經(jīng)是最頂項(xiàng)了!');
return false;
}
var upStep = currentStep - 1;
//修改序號
$('#line' + upStep + " td:first-child").html(currentStep);
$('#line' + currentStep + " td:first-child").html(upStep);
//取得兩行的內(nèi)容
var upContent = $('#line' + upStep).html();
var currentContent = $('#line' + currentStep).html();
$('#line' + upStep).html(currentContent);
//交換當(dāng)前行與上一行內(nèi)容
$('#line' + currentStep).html(upContent);
$('#content tr').each(function () { $(this).css("background-color", "#ffffff"); });
$('#line' + upStep).css("background-color", "yellow");
event.stopPropagation(); //阻止事件冒泡
}
//下移
function down_exchange_line(index) {
if (index != null) {
currentStep = $(index).parent().parent().find("td:first-child").html();
}
if (currentStep == 0) {
alert('請選擇一項(xiàng)!');
return false;
}
if (currentStep >= max_line_num) {
alert('已經(jīng)是最后一項(xiàng)了!');
return false;
}
var nextStep = parseInt(currentStep) + 1;
//修改序號
$('#line' + nextStep + " td:first-child").html(currentStep);
$('#line' + currentStep + " td:first-child").html(nextStep);
//取得兩行的內(nèi)容
var nextContent = $('#line' + nextStep).html();
var currentContent = $('#line' + currentStep).html();
//交換當(dāng)前行與上一行內(nèi)容
$('#line' + nextStep).html(currentContent);
$('#line' + currentStep).html(nextContent);
$('#content tr').each(function () { $(this).css("background-color", "#ffffff"); });
$('#line' + nextStep).css("background-color", "yellow");
event.stopPropagation(); //阻止事件冒泡
}
//保存數(shù)據(jù)
function SaveData() {
var data = "<root>";
$('#content tr').each(function () {
data += "<item>";
var stepName = $(this).find("td:eq(1)").find("input").val();
var stepDescription = $(this).find("td:eq(2)").find("input").val();
data += " <stepName>" + stepName + "</stepName>";
data += " <stepDescription>" + stepDescription + "</stepDescription>";
data += "<item>";
});
data += "</root>";
alert(data);
}
</script>
使用本站在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun,測試運(yùn)行上述代碼可得到如下運(yùn)行效果:

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery表格(table)操作技巧匯總》、《jQuery操作xml技巧總結(jié)》、《jQuery form操作技巧匯總》、《jQuery常用插件及用法總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計(jì)有所幫助。
- jQuery實(shí)現(xiàn)獲取table表格第一列值的方法
- JQuery Ajax動(dòng)態(tài)生成Table表格
- jQuery對table表格進(jìn)行增刪改查
- 基于JQuery的動(dòng)態(tài)刪除Table表格的行和列的代碼
- JQuery 動(dòng)態(tài)生成Table表格實(shí)例代碼
- 使用jQuery操作HTML的table表格的實(shí)例解析
- jQuery實(shí)現(xiàn)Table表格隔行變色及高亮顯示當(dāng)前選擇行效果示例
- jQuery實(shí)現(xiàn)table表格checkbox全選的方法分析
- jQuery+CSS實(shí)現(xiàn)的table表格行列轉(zhuǎn)置功能示例
- jQuery實(shí)現(xiàn)table表格信息的展開和縮小功能示例
- jQuery實(shí)現(xiàn)為table表格動(dòng)態(tài)添加或刪除tr功能示例
相關(guān)文章
jQuery學(xué)習(xí)筆記之創(chuàng)建DOM元素
這篇文章主要介紹了jQuery學(xué)習(xí)筆記之創(chuàng)建DOM元素的相關(guān)資料,需要的朋友可以參考下2015-01-01
jquery實(shí)現(xiàn)可點(diǎn)擊伸縮與展開的菜單效果代碼
這篇文章主要介紹了jquery實(shí)現(xiàn)可點(diǎn)擊伸縮與展開的菜單效果代碼,涉及jquery鼠標(biāo)click事件控制頁面元素樣式變換的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
JQuery EasyUI Layout 在from布局自適應(yīng)窗口大小的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狫Query EasyUI Layout 在from布局自適應(yīng)窗口大小的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05
jquery利用event.which方法獲取鍵盤輸入值的代碼
jquery利用event.which方法獲取鍵盤輸入值的代碼,需要的朋友可以參考下。2011-10-10
jQuery獲取table行數(shù)并輸出單元格內(nèi)容的實(shí)現(xiàn)方法
這篇文章主要介紹了jQuery獲取table行數(shù)并輸出單元格內(nèi)容的實(shí)現(xiàn)方法,涉及jQuery針對表格與頁面元素的動(dòng)態(tài)操作技巧,需要的朋友可以參考下2016-06-06
Jquery修改頁面標(biāo)題title其它JS失效的解決方法
這篇文章主要介紹了Jquery修改頁面標(biāo)題title其它JS失效的解決方法,很簡單,很實(shí)用,需要的朋友可以參考下2014-10-10
jQuery ajax提交Form表單實(shí)例(附demo源碼)
這篇文章主要介紹了jQuery ajax提交Form表單的方法,結(jié)合實(shí)例分析了jQuery ajax操作實(shí)現(xiàn)表單提交的相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-04-04

