添加tr标签会导致功能停止工作标签、功能、工作、tr

2023-09-10 21:08:19 作者:浅奏離歌

我有低于此功能才能正常工作:

I have a function below which works correctly:

function insertQuestion(form) {   
    var $tbody = $('#qandatbl > tbody'); 
    var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
    var $noofanswers = $("<td class='noofanswers'></td>");
    var $questionType = '';

    $('.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);
    }); 

    $tr.append($noofanswers);
    $tbody.append($tr); 
}

不过,我想 $ noofanswers 将包含在它自己的表行,所以我再改 $ noofanswers 包括&LT; TR&GT;&LT; / TR&GT; 使code:

But I wanted $noofanswers to be included in it's own table row so I then change $noofanswers to include <tr></tr> so that code:

var $noofanswers = $("<td class='noofanswers'></td>");

现在变成了:

var $noofanswers = $("<tr><td class='noofanswers'></td></tr>");

但是,如果我这样做,那么 $('。numberAnswerTxt,背景)。每个(函数(){不正确不是那么回事。所以我的问题是,如果我添加了&LT; TR&GT; 标签,那什么,我需要在函数中改变让它再次合作

But if I do this then $('.numberAnswerTxt', context).each(function() { doesn't quite work correctly. So my question is that if I add the <tr> tags, then what do I need to change within the function to get it working again?

推荐答案

insertQuestion 功能已经包装了 D 在元素 TR

The insertQuestion function already wraps the td element in a tr:

var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
//...
$tr.append($noofanswers);

您可以简单地修改 TR $ TR ,而不是增加 $ noofanswers ,或者你可以删除调用追加并保持 TR $ noofanswers

You could simply modify $tr instead of adding the tr to $noofanswers, or you could remove the call to append and keep the tr in $noofanswers.

//var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
var $noofanswers = $("<tr><td class='noofanswers'></td></tr>");
//...
//$tr.append($noofanswers);
$tbody.append($noofanswers);