1.我的功能需求是:"問卷調(diào)查" , 庫表有三張,一個(gè)存問卷調(diào)查的標(biāo)題,一個(gè)存儲問卷調(diào)查的題目,一個(gè)是存問卷調(diào)查的題目對應(yīng)的選項(xiàng).2.個(gè)人難題: 如何將標(biāo)題,題目和選項(xiàng)通過一個(gè)jsp頁面存到三張庫表中.3.存題目的表和存選項(xiàng)的表是1對多的關(guān)系
3 回答

HUWWW
TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊
form表單提交,后臺用實(shí)體接收的方式,有點(diǎn)繁瑣
我當(dāng)時(shí)是前臺將數(shù)據(jù)拼成json,轉(zhuǎn)換成字符串傳到后臺;
后臺解析為json對象,然后存儲對應(yīng)字段到對應(yīng)的表中

尚方寶劍之說
TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊
假設(shè)你的html如下:
<div class="content">
<div class="title">
<span>標(biāo)題:</span><input type="text" class="title"/>
</div>
<div class="list">
<div class="item">
<span>題目:</span><input type="text" class="question"/>
<span>選項(xiàng):</span><input type="text" class="option"/>
<span>選項(xiàng):</span><input type="text" class="option"/>
<span>選項(xiàng):</span><input type="text" class="option"/>
<span>選項(xiàng):</span><input type="text" class="option"/>
</div>
<div class="item">
<span>題目:</span><input type="text" class="question"/>
<span>選項(xiàng):</span><input type="text" class="option"/>
<span>選項(xiàng):</span><input type="text" class="option"/>
<span>選項(xiàng):</span><input type="text" class="option"/>
<span>選項(xiàng):</span><input type="text" class="option"/>
</div>
</div>
</div>
你可以用js或者jquery,將問卷當(dāng)做一個(gè)js對象,然后格式化為json字符串進(jìn)行傳輸。
<script type="text/javascript">
var title = $('.content .title input').val();
// 獲取<div class='item'>標(biāo)簽數(shù)組
var itemList = $('.content div.list').children();
var itemArray = [];
for (int i = 0; i < itemList.length; i++) {
var question = itemList[i].class('.question').val();
var options = [];
itemList.class('option').each(function(index, value){
options.push($(this).val());
});
itemArray.push({question: question, option: options});
}
// 得到的對象格式形如:
/*{
title: "title",
question: [
{
question: "Which kind of animal do you like most?",
option: ["horse", "koala", "rabbit", "panda"]
},
{
question: "Which kind of animal do you like most?",
option: ["horse", "koala", "rabbit", "panda"]
}
]
}*/
var combinedParam = {title: title, question: itemArray};
// 將combinedParam轉(zhuǎn)為JSON字符串傳給后臺
// ...
</script>
我不懂js所以以上的代碼你權(quán)當(dāng)偽代碼看。
java端獲取JSON字符串以后,反序列化得到JSON對象,然后根據(jù)需要,該怎么存表就怎么存。

蝴蝶刀刀
TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊
用js或者ajax實(shí)現(xiàn),點(diǎn)擊一個(gè)按鈕,觸發(fā)三個(gè)函數(shù)或者ajax
添加回答
舉報(bào)
0/150
提交
取消