Any ideas as to why the ID and Name are not updating here?
function addQuestion(){
var currentQuestion = $(".questionContainer").size(); //Number of questionContainers
var $newElement = window.$pQuestion.clone().attr('id','questionContainer'+currentQuestion);
$newElement.children('#question0').attr('id','question'+currentQuestion).attr('name','quest ion'+currentQuestion);
//Update the answerContainer div id.
$newElement.children('#answerContainer0').attr('id','answerContainer'+current开发者_如何学PythonQuestion);
//Update the first answer id and name
var answerId = 'question'+currentQuestion+'-answer1';
$newElement.children('#question0-answer1').attr("id",answerId).attr("name",answerId);
$newElement.appendTo('#questionArea');
}
The last few lines is where the issue seems to be. This should update to "question1-answer1" I have verified that the value of answerId is correct, just seems to not be applied to the ID and Name.
Let me know if you'd like more context.
Is the #question0-answer1
an immediate child of the .questionContainer
in the DOM ?
Or might it be nested deeper ? in which case you would need to use
$newElement.find('#question0-answer1').attr("id",answerId).attr("name",answerId);
update
seeing the posted code, indeed your element is not a child but a descendant, so use the suggestion above..
try to insert this few alerts
//Update the first answer id and name var answerId = 'question'+currentQuestion+'-answer1'; // this should display the expected id alert(answerId); // the display of this should be greater than zero else // #question0-answer1 is not an immediate child of $newElement alert($newElement.children('#question0-answer1').length()); $newElement.children('#question0-answer1').attr("id",answerId).attr("name",answerId); $newElement.appendTo('#questionArea');
I would recommend you to use .find() instead of .children()
$newElement.find('#question0-answer1').blah.blah;
精彩评论