jquery - 它为每行显示一个 <tbody>

标签 jquery

我的表的 jquery 解析有问题。我相信我现在的解析是这样的:

<table id="qandatbl">
<thead>
<tr>
    <th class="qid">Question No</th>
    <th class="question">Question</th>
    <th class="option">Option Type</th>
    <th class="noofanswers">Number of Answers</th>
    <th class="answer">Answer</th>
    <th class="noofreplies">Number of Replies</th>
    <th class="weight">Number of Marks</th>
    <th class="image">Image</th>
    <th class="video">Video</th>
    <th class="audio">Audio</th>
</tr>
</thead>
<tbody>
<tr></tr>
</tbody>
<tbody>
<tr></tr>
</tbody>
<tbody>
<tr></tr>
</tbody>
</table>

它正在创建一个<tbody>对于每一行。相反,它应该显示一个 <tbody>并显示所有<tr><tbody> 。我想要如下所示:

<table id="qandatbl">
<thead>
<tr>
    <th class="qid">Question No</th>
    <th class="question">Question</th>
    <th class="option">Option Type</th>
    <th class="noofanswers">Number of Answers</th>
    <th class="answer">Answer</th>
    <th class="noofreplies">Number of Replies</th>
    <th class="weight">Number of Marks</th>
    <th class="image">Image</th>
    <th class="video">Video</th>
    <th class="audio">Audio</th>
</tr>
</thead>
<tbody>
<tr></tr>
<tr></tr>
<tr></tr>
</tbody>
</table>

如何实现上述目标?

下面是控制<tbody>的jquery代码和<tr> .

    function insertQuestion(form) {   

        var $tbody = $("<tbody class='tbody'></tbody>");
        var $tr = $("<tr class='optionAndAnswer'></tr>");
        var $qid = $("<td class='qid'>" + qnum + "</td>");
        var $question = $("<td class='question'></td>");
        var $noofanswers = $("<td class='noofanswers'></td>");
        var $options = $("<td class='option'></td>");
        var $answer = $("<table class='answer'></table>");
        var $replies = $("<td class='noofreplies'><div class='wrapper'></div></td>");
        var $weight = $("<td class='weight'></td>");
        var $image = $("<td class='image'></td>");
        var $video = $("<td class='video'></td>");
        var $audio = $("<td class='audio'></td>");

    $('.gridTxt', context).each( function() {

     var $this = $(this);
     var $optionsText = $("<input type='text' class='gridTxtRow maxRow' readonly='readonly' /><span href='#' class='showGrid'>[Open Grid]</span>").attr('name',$this.attr('name'))
                     .attr('value',$this.val())

    $options.append($optionsText);
    $questionType = $this.val();

    });

   $('.numberAnswerTxt', context).each(function() {
        var $this = $(this);
        var $noofanswersText = '';

        if ($questionType == 'True or False' || $questionType == 'Yes or No'){

    $noofanswersText = $("<span class='naRow string' style='display: block;'>Only 1 Answer</span><input type='text' class='numberAnswerTxtRow answertxt' style='display: none;' onkeyup='numberKeyUp(this)' onkeypress='return isNumberKey(event)' onChange='getButtons()'>").attr('name', $this.attr('name')).attr('value', $this.val())

    }else{

    $noofanswersText = $("<span class='naRow string' style='display: none;'>Only 1 Answer</span><input type='text' class='numberAnswerTxtRow answertxt' style='display: block;' onkeyup='numberKeyUp(this)' onkeypress='return isNumberKey(event)' onChange='getButtons()'>").attr('name', $this.attr('name')).attr('value', $this.val())   

    }

        $noofanswers.append($noofanswersText);

        }); 


    $('#questionTextArea').each( function() {

    var $this = $(this);
    var $questionText = $("<textarea onload='resizeTxt'></textarea>").attr('name',$this.attr('name'))
                   .attr('value',$this.val())

    $question.append($questionText);



        $tbody.append($tr); 
        $tr.append($qid);
        $tr.append($question);
        $tr.append($options);
        $tr.append($noofanswers);
        $tr.append($answer);
        $tr.append($replies);
        $tr.append($weight);
        $tr.append($image);
        $tr.append($video);
        $tr.append($audio);    
        $('#qandatbl').append($tbody);

    }

下面是整理 <thead> 的 html以及 jquery <tbody> 所在的表和<tr>将进入:

<table id="qandatbl" align="center">
<thead>
<tr>
    <th class="qid">Question No</th>
    <th class="question">Question</th>
    <th class="option">Option Type</th>
    <th class="noofanswers">Number of Answers</th>
    <th class="answer">Answer</th>
    <th class="noofreplies">Number of Replies</th>
    <th class="weight">Number of Marks</th>
    <th class="image">Image</th>
    <th class="video">Video</th>
    <th class="audio">Audio</th>
</tr>
</thead>
</table>

最佳答案

您只需添加 <tbody>到 html:

<table id="qandatbl" align="center">
   <thead>
    <tr>
       <th class="qid">Question No</th>
       <th class="question">Question</th>
       <th class="option">Option Type</th>
       <th class="noofanswers">Number of Answers</th>
       <th class="answer">Answer</th>
       <th class="noofreplies">Number of Replies</th>
       <th class="weight">Number of Marks</th>
       <th class="image">Image</th>
       <th class="video">Video</th>
       <th class="audio">Audio</th>
    </tr>
   </thead>
   <tbody class="tbody"></tbody>
 </table>

然后使用这个:

function insertQuestion(form) {   

var $tbody = $('#qandatbl tbody');

//rest of your function

http://jsfiddle.net/QGBw3/1/

关于jquery - 它为每行显示一个 <tbody>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8958225/

相关文章:

javascript - 使用 jQuery 向输入字段添加最大值

javascript - Spring Portlet Jquery Ajax 发布到 Controller

jQuery.css() - marginLeft 与 margin-left?

javascript - 基于来自 Dom 元素的 CSS 路径模拟点击

javascript - Scrollspy 效果停止工作

javascript - 使用 JQuery.inArray() 查找字符串

javascript - fullCalendar - 禁用多选日

javascript - 速度和内存受益于在 JavaScript 循环外声明变量?

javascript - 在 for 循环中创建 .on() 元素时,如何使 .on(function) 调用当前索引?

javascript - 如何从另一个 html 文件更改视频 attr src