V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
scott123
V2EX  ›  Python

一个多行多列的表格大表单数据,并且行数是不确定的会根据用户自己添加行数确定,怎么实现很好的获取所有提交数据,我准备使用 ajax 提交数据,如何设计实现?

  •  
  •   scott123 · 2016-06-19 21:15:40 +08:00 · 4193 次点击
    这是一个创建于 2873 天前的主题,其中的信息可能已经有所发展或是发生改变。
    				<div class="col-md-11 form-table">
    					<table class="table table-bordered table-hover">
    						<thead>
    							<tr>
    								<th>
    									Category
    								</th>
    								<th>
    									Model
    								</th>
    								<th>
    									Asset_ID
    								</th>
    								<th>
    									SN
    								</th>
    								<th>
    									Qty
    								</th>
    							</tr>
    						</thead>
    						<tbody id="suggest1">
    							<tr>
    								<td>
    									<input type="text" name=""  value="" />
    								</td>
    								<td>
    									<input type="text" name="" id="" value="" />
    								</td>
    								<td>
    									<input type="text" />
    								</td>
    								<td>
    									<input type="text" />
    								</td>
    								<td>
    									<input type="text" />
    								</td>
    							</tr>
    								<td>
    									<input type="text" name="" id="" value="" />
    								</td>
    								<td>
    									<input type="text" name="" id="" value="" />
    								</td>
    								<td>
    									<input type="text" />
    								</td>
    								<td>
    									<input type="text" />
    								</td>
    								<td>
    									<input type="text" />
    								</td>
    							</tr>
                        
                            
    						</tbody>
    					</table>
    				</div>
    		</div>
    
    8 条回复    2016-06-23 08:28:11 +08:00
    hrong
        1
    hrong  
       2016-06-19 22:14:12 +08:00 via Android
    看你用什么后端了?前端就 jqgrid?
    murmur
        2
    murmur  
       2016-06-19 22:44:39 +08:00
    现在用 jquery easyui 的很多吧 ie6-ie11 兼容性都很好 企业开发几乎首选了
    rekulas
        3
    rekulas  
       2016-06-19 22:50:26 +08:00
    设计什么?动态添加表单、 ajax 数据提交教程一大把有什么好需要设计的。。
    scott123
        4
    scott123  
    OP
       2016-06-19 22:55:53 +08:00
    我现在不太清楚的是,我有一个固定四列,动态 n 行的 input 表格,当用户将数据填满的时候,提交数据的时候,我以怎么方式,把每一行的数据安装对应的列的值的取得,而且还是安装顺序的,然后我会存在一个字典里,然后我会用 ajax ,将数据创给后台。有木有相关的代码参考一下。
    jugelizi
        5
    jugelizi  
       2016-06-19 22:59:45 +08:00
    所以你是不知道 input name="field[]" ?
    scott123
        6
    scott123  
    OP
       2016-06-19 23:05:32 +08:00
    我一个数据表格可能有三四十个 input ,像我上面写的,都是相同规律的,没有什么简单的方法?类似使用 each 方法搞个双循环,写一个函数可以直接获得数据到一个字典。
    xmh51
        7
    xmh51  
       2016-06-19 23:12:48 +08:00
    @scott123 jquery 不是能获得当前节点下所有的 input 元素吗? 遍历不行吗?
    scott123
        8
    scott123  
    OP
       2016-06-23 08:28:11 +08:00
    function GetReceivedItemData(){
    var item_time= 0;
    var item = {
    'category':'',
    'model':'',
    'asset_id':'',
    'sn':'',
    'qty':'',
    };
    var item_list_array={'item_data':[]};
    $('#suggest1 input').each(function (index, value) {
    //console.log('div' + index + ':' + $(this).val());
    if(index%5==0){
    item['category']=$(this).val();
    }
    if(index%5==1){
    item['model']=$(this).val();
    }
    if(index%5==2){
    item['asset_id']=$(this).val();
    }
    if(index%5==3){
    item['sn']=$(this).val();
    }
    if(index%5==4){
    item['qty']=$(this).val();
    if(is_valid_data(item)){
    item_list_array['item_data'].push(JSON.stringify(item));
    item_time+=1;
    }
    else{
    if(is_empty_data(item,this)){
    return true;
    }
    else{
    alert('The '+[item_time+1]+' item data is invalid.');
    return false;
    }
    };
    }
    });
    //console.log(item_list_array['item_data']);
    return item_list_array;
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1758 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 00:43 · PVG 08:43 · LAX 17:43 · JFK 20:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.