Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery、ID和Name属性未更新_Jquery - Fatal编程技术网

JQuery、ID和Name属性未更新

JQuery、ID和Name属性未更新,jquery,Jquery,你知道为什么这里没有更新ID和名称吗 function addQuestion(){ var currentQuestion = $(".questionContainer").size(); //Number of questionContainers var $newElement = window.$pQuestion.clone().attr('id','questionContainer'+currentQuestion); $newElement.children('#questio

你知道为什么这里没有更新ID和名称吗

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'+currentQuestion);

//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');
}
最后几行是问题所在。这应该更新为“question1-answer1”。我已经验证了answerId的值是正确的,只是似乎没有应用于ID和名称

如果需要更多上下文,请告诉我。

问题0-answer1是DOM中
.questionContainer
的直接子项吗

或者它可能嵌套得更深?在这种情况下,您需要使用

$newElement.find('#question0-answer1').attr("id",answerId).attr("name",answerId);

更新


看到发布的代码,确实您的元素不是子元素而是子元素,因此请使用上面的建议。

尝试插入这几个警报

//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');
你怎么知道他们没有更新?请记住,并非所有DOM检查器都会在JavaScript更改属性时更新。最好的办法是在设置属性后将其清除,以便检查。1问题(可能只是粘贴的代码中的问题)是第四行的
'quest ion'+currentQuestion
。。删除空格。。另外,如果你可以发布相关的html(一个问答组),你可以在jsfiddle.net上创建一个fiddle来显示标记吗?@Gaby,是的,只是粘贴代码:)-这是fiddle@Ben I did alert(),答案ID是正确的,有没有办法提醒()$newelement的属性感谢ian,加比很快就发现了这一点,所以我将他的答案标记为绿色。欢迎您找到答案:)
$newElement.find('#question0-answer1').blah.blah;