使用jQuery从表和包中将信息提取到JSON对象中

时间:2015-07-27 17:21:14

标签: javascript jquery json html-table

我有一个任务,当用户点击提交并打包到JSON对象中时,我会要求从表中提取几个信息(使用jQuery),这些信息将存储在数据库中。

jsfiddle链接:https://jsfiddle.net/ek30dgt9/2/

这是我的HTML代码:

    <body>

    <div >

    <table border=1px>

  <thead>
    <tr>
        <th colspan="5">Description</th>
        <th>Comment</th>
        <th>User</th>
        <th>Feedback</th>
        <th>Status</th>
    </tr>
    </thead>
        <tbody style="border-top : 1px solid black" id = "1">
    <tr>
        <td class="partition"><label class="label">Title:</label><label class="label ">A</label></td>
        <td class="sip" style="border-left:none"><label class="label">Author:</label><label class="label">James</label></td>
        <td rowspan="4"><input type="checkbox"><label class="label" style="padding-left:0px">Ordinary</label></td>
        <td rowspan="4"><input type="checkbox"><label class="label" style="padding-left:0px">Interesting</label></td>
        <td rowspan="4"><input type="checkbox"><label class="label" style="padding-left:0px">Amazing</label></td>
        <td rowspan="4"><textarea maxlength="180" class="animated" id="usercomment" name="comment" style="overflow: hidden; word-wrap: break-word; resize: horizontal;  height: 80px; width:280px;">The book is ..........</textarea></td>
        <td rowspan="4" align="center">Adam<br><button class="label label-primary">Submit</button></td>
        <td rowspan="4" align="center">Feedback Goes Here<br></td>
            <td rowspan="4" align="center"><br>No Feedback Yet</td>
    </tr>
    <tr>
        <td colspan="2" class="def"><label class="label">Genre:</label><label class="label">Fiction</label></td>
    </tr>
    <tr>
        <td colspan="2" class="path"><label class="label label-primary" style="margin-left:5px">BookURL</label><label class="label label-primary " style="margin-left:40px">DownloadLink</label></td>
    </tr>
    <tr>
        <td colspan="2" class="path"><a style="display:none">www.ddd.com/bookurl</a></td>
    </tr>
</tbody>

我对如何使用jQuery提取所有信息没有任何想法。

我的JSON对象必须是这样的:

{
"tbodyid":"1",
"title":"A",
"author":"James",
"genre":"fiction",
"bookurl":"www.bllllll.com",
"downloadlink":"www.zzzz.com",
"source":
{
    "selection":"library",
    "checked":"true",
}
"comment":"comment_goes_here",
"user":"Adam",
"feedback":"feedback_from_lecturer_goes_here",
"status":"no feedback yet",
}

p / s:允许用户为源选择多个选项。例如:图书馆和电子书。

1 个答案:

答案 0 :(得分:0)

您可以为需要提取的元素添加id属性:

<tr>
    <td class="partition"><label class="label" id='title'>Title:</label><label class="label ">A</label></td>
    <td class="sip" style="border-left:none"><label class="label" id='author'>Author:</label><label class="label">James</label></td>
...
</td>

然后,使用JQuery:

function foo() {
    return {
        "title": $("#title").html(),
        "author": $("#author").html()
        ...
    }
}