English 中文(简体)
添加tr标记会导致函数停止工作
原标题:adding tr tag causes function to stop working

我下面有一个功能可以正常工作:

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包含在它自己的表行中,所以我将$noofansswers更改为包含<;tr></tr>,使代码:

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

现在变成:

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

但如果我这样做,那么$(.numberAnswerTxt,context).each(function(){就不能正常工作。所以我的问题是,如果我添加<;tr>;标记,那么我需要在函数中更改什么才能使其重新工作?

问题回答

insertQuestion函数已经将td元素包装在tr中:

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

您可以简单地修改$tr,而不是将tr添加到$soofanswers中,或者您可以删除对append的调用,并将tr%noofanswers中。

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

你有

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

以及以后

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

因此,您不能用另一个tr包装您的td,因为它已经被包装了。然后您应该这样更改代码:

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




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!