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