我在codeigniter中有以下视图-说edit_res_view-
<html>
<table>
<? foreach ($list as $key => $value): ?>
<tr><td><?= $key ?> :</td><td><input type="text" name='<?= $key ?>' size="25" value='<?php echo $value; ?>'/></td></tr>
<? endforeach; ?>
<tr><td colspan="2"><center><input type="submit" id="submit" size="40" value="SAVE" />
</table>
现在我在另一个视图中有了另一个jquery函数它从这个表单的name属性中获取值作为POST数据发送给另一个函数-
$("#submit").live("click",function(){
$.ajax({
url: "http://localhost/codeigniter_local/index.php/manage_resources/doupdate_des",
type:"POST",
data:"d_id="+$("[name='d_id']").val()+
"&cpu_brand="+$("[name='cpu_brand']").val()+
"&processor="+$("[name='processor']").val()+
"&ram="+$("[name='ram']").val()+
"&hdd="+$("[name='hdd']").val()+
"&mac_id_wired="+$("[name='mac_id_wired']").val()+
"&mac_id_wireless="+$("[name='mac_id_wireless']").val()+
"&os="+$("[name='os']").val()+
"&os_license_no="+$("[name='os_license_no']").val()+
"&monitor_sno="+$("[name='monitor_sno']").val()+
"&keyboard_sno="+$("[name='keyboard_sno']").val()+
"&mouse_sno="+$("[name='mouse_sno']").val()+
"&sno="+$("[name='sno']").val()+
"&date_of_purchase="+$("[name='date_of_purchase']").val()+
"&warranty="+$("[name='warranty']").val()+
"&comments="+$("[name='comments']").val(),
success:function(html){
oTable.fnReloadAjax('http://localhost/codeigniter_local/index.php/manage_resources/json/1');
alert("Done !");
$("#form1").dialog("close");
},
ERROR: function(html){
alert(html);
}
});
} );//for submit
现在的问题是,我想循环jquery函数的"data:"属性的值,而不是像上面那样硬编码它。
怎么做?(顺便说一句,$list数组通过我的控制器作为参数传递给edit_res_view)。
类似的东西应该使用jQuery map
函数工作:
var postData = $("#form1 input:text").map ( function (J, node) {
var jThis = $(node);
return jThis.attr ('name') + '=' + jThis.val ();
} ).get ().join ('&');
$.ajax({
url: "http://localhost/codeigniter_local/index.php/manage_resources/doupdate_des",
type: "POST",
data: encodeURI (postData)
... ...
该代码假设表单的id为"form1",并帮助确保只使用正确的表单输入。