簡體   English   中英

在php中創建復選框時,ajax在接收數據時僅在div中顯示一個復選框。

[英]On creating checkbox in php, only Only one checkbox is displayed in the div by ajax on receiving the data.

我已經在php中創建了復選框:

    $check =array();
    $id=1;
    while($result = $query->fetch(PDO::FETCH_ASSOC)){
        $check[]= "<input id='checkoverlay".$id."' name='overlaycheckbox' 
        value='overlaycheck'  class='olButton' type='checkbox'>
        <label class='labelSpan olButton' style='vertical-align: bottom'>".$result['title']."<br>";
        $id++;
}
echo json_encode($check);

當我以ajax接收數據時,我得到一個數組,我再次循環提取我在php中成功創建的復選框。 但是問題是只顯示了1個復選框:最后一個。 也許該變量正在獲取最新值,並顯示該值以擦除較早的值,但我無法獲取其他數據。

    $.ajax({
            type:"Post",
            url:"query.php",
            dataType:"json",
            success: function(data){
                for(i=0;i<data.length;i++){ 
                    check= data[i];
                    console.log(check);
                    $('.div').html(check);
                 }
            }
});

這是因為您每次都覆蓋數據。 您需要使用.append()而不是.html() 因此,如下更改您的ajax代碼:

$.ajax({
            type:"Post",
            url:"query.php",
            dataType:"json",
            success: function(data){
                $('.div').html(""); //initialize
                for(i=0;i<data.length;i++){ 
                    check= data[i];
                    console.log(check);
                    $('.div').append(check);
                 }
            }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM