最近一直在學習coldfusion,今天遇到了一個動態增行,並進行批量保存的問題,搞了半天終於搞定啦,發出來和大家分享一下。
以下是具體代碼,感興趣的朋友可以看看:
<cfprocessingdirective pageencoding="utf-8"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="webroot/js/jquery.js"></script> <script> function addCustomField(id){ val = parseInt(Math.random()*100); tr_id = $("#test>tbody>tr:last").attr("id"); tr_id++; str = "<tr><td><input type='text' name='username_"+val+"'/><input type='password' name='userpass_"+val+"' /></td></tr>"; $('#'+id).append(str); } </script> <cfif isDefined("form.submit")> <cfif isdefined("form.fieldnames")> <cfif form.fieldNames CONTAINS "username_"> <cfloop list="#form.fieldnames#" index="i"> <cfif i CONTAINS "username_"> <cfset fieldid = listRest(i,"_")> <cfquery name="batch" datasource="#request.dsn#"> insert into cf_user(name,pass) values( '#evaluate("form.username_" & fieldid)#', '#evaluate("form.userpass_" & fieldid)#' ) </cfquery> </cfif> </cfloop> </cfif> </cfif> </cfif> <title>無標題文檔</title> </head> <body> <cfform> <table id="cust_table"> <tr> <td>用戶註冊</td> </tr> <tr> <td> <input type="text" name="username_1" /> <input type="password" name="userpass_1" /> </td> <td><img src="images/admin_add.jpg" onclick="addCustomField('cust_table');"/></td> </tr> <input type="submit" name="submit" value="註冊" /> </table> </cfform> </body> </html>
數據庫當然還是最簡單的用戶表,只有三個字段:
create table cf_user(
id int primary key auto_increment,
name varchar(20),
pass varchar(20)
)
以上就是全部的信息啦,感興趣的朋友可以一起交流coldfusion的信息,本人QQ:395079181