我试图在PHP中使用$ .post在jquery中附加到<ul>
,但输出为空,我如何将li
数据附加到ul
}?
我的PHP
$queryStmt = 'SELECT test_part_id FROM test_part WHERE test_id=:sqlId';
$queryPrepare = $dba_connect->prepare($queryStmt);
$queryPrepare->execute(array(':sqlId'=>$_POST['testId']));
$queryResult = $queryPrepare->fetchAll(PDO::FETCH_ASSOC);
$queryPrepare->closeCursor();
$tempString = '';
$tempNum = 1;
foreach($queryResult as $row) {
$tempString .= "<li><span class=''></span>
<button id='".$row['test_part_id']."'
name='".$row['test_part_id']."'
type='button'
class='listPartOfExam'>".
"Part ".$tempNum."</button></li>";
$tempNum++;
}
$tempString .= "<li><span class='red'> </span> button id='btnTeachLogout' name='btnTeachLogout' type='button'>Logout</button></li> ";
echo $tempString;
我的Jquery
$.post(
'../php/teacherfunction.php',
{'functions':'generatePartsButton','testId':localStorage.inTestid},
function(data) {
alert(data);
$('#ulEditExamPartsMenu ul').append(data);
}
);
和html代码:
<div id='divEditTestMainMenu' hidden=''>
<ul id='ulEditExamPartsMenu' class='coolHandLuke'>
</ul>
</div>
I use the alert above to see what is inside the data, and it is correct:
<li><span class=''></span>
<button id='TC-201402150304203041_1' name='TC-201402150304203041_1'
type='button' class='listPartOfExam'>Part 1</button></li>
<li><span class=''></span>
<button id='TC-201402150304203041_2' name='TC-201402150304203041_2'
type='button' class='listPartOfExam'>Part 2</button></li>
<li><span class=''></span>
<button id='TC-201402150304203041_3' name='TC-201402150304203041_3'
type='button' class='listPartOfExam'>Part 3</button></li>
<li><span class='red'> </span> button id='btnTeachLogout'
name='btnTeachLogout' type='button'>Logout
</button></li>
答案 0 :(得分:2)
这是在这个元素ul
中寻找ulEditExamPartsMenu
,它本身就是ul
:
$('#ulEditExamPartsMenu ul').append(data);
所以改变应该像:
$('#ulEditExamPartsMenu').append(data);
答案 1 :(得分:1)
ulEditExamPartsMenu
是ul
所以
$('#ulEditExamPartsMenul').append(data);
您的选择器#ulEditExamPartsMenu ul
正在寻找ul
元素,该元素是ID为ulEditExamPartsMenu
的元素的后代,这样的元素不存在,因此不会附加任何内容。
答案 2 :(得分:1)
替换此行
$('#ulEditExamPartsMenu ul').append(data);
with,
$('#ulEditExamPartsMenu').append(data);
或
$('ul#ulEditExamPartsMenu').append(data);
原因是 ulEditExamPartsMenu 是ul
本身的ID
在<
关闭
</span>
下面的行$tempString .= "<li><span class='red'> </span> button id='btnTeachLogout' name='btnTeachLogout' type='button'>Logout</button></li> ";
{{1}}