jquery没有附加到ul

时间:2015-03-04 12:37:37

标签: php jquery

我试图在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>

3 个答案:

答案 0 :(得分:2)

这是在这个元素ul中寻找ulEditExamPartsMenu,它本身就是ul

$('#ulEditExamPartsMenu ul').append(data);

所以改变应该像:

$('#ulEditExamPartsMenu').append(data);

答案 1 :(得分:1)

ulEditExamPartsMenuul所以

的ID
$('#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}}