2016-12-04 40 views
0

我在codeigniter框架中使用jquery ajax來加載彈出模式按鈕單擊。我將ajax請求傳遞給控制器​​中的某個函數,並接收一些數據,這些數據應該以我彈出的模式顯示。模式標籤的數量取決於ajax請求接收的數組大小。使用for循環插入多個標籤字段

我不知道如何做到這一點。但我嘗試將接收數組的大小傳遞給彈出模式上創建的窗體中的隱藏類型輸入字段。

以下是我的javascript。

<script type="text/javascript"> 

$(document).ready(function() { 

    $(document).on('click','#btn_more', function() { 
    empId = $('#employeeId').html(); 
    fiter_employees(empId); 
    }); 

    function fiter_employees(empId){ 

    var empSet ={ 
     empId: empId, 
     method: 'ajax' 
    }; 

    var empSentUrl = 'http://localhost/eventmanagementsystem/index.php/employee/get_emp_positions'; 

    $.ajax({ 
     type: 'POST', 
     dataType: 'json', 
     url: empSentUrl, 
     data: empSet, 

     success: function(data){ 

     $('#modal_pkg1').html(data.empPosition.length) 

     $('#employeePositions').modal(); 
     } 
    }); 
    } 
}); 

在我彈出的模式,我用它沒有成功以下代碼。

<div id="employeePositions" class="modal fade"> 
<div class="modal-dialog" role="document"> 
<div class="modal-content"> 

    <div class="modal-header"> 
    <h4 class="modal-title">Employee Name :</h4><br/> 

    <form> 
    <?php 
    for ($i=0; $i < ?> 
    <input type="hidden" id="modal_pkg1" value="modal_pkg1" /> 
    <?php 
    ; $i++) { ?> 
    <h4 class="modal-title" id="event_name"></h4> 
    <?php 
    } ?> 
    </form> 

有人能告訴我這樣做,請正確我...

回答

0

你的PHP代碼執行加載頁面的時刻,所以當你改變modal_pkg1價值,什麼都不會發生。 此外,foor循環本身不會在加載時工作,因爲它期望一個不存在的數字。

我建議你在JS中完全做到這一點,從你的腳本中刪除所有的PHP代碼;類似如下:

在你成功的Ajax功能

.success(function(data){ 
    var htmlStr = '' 
    for(var i=0;i<data.empPosition.length;i++){ 
     htmlStr+='<whatever html element you want to display>' 
    } 
    $('form').html(htmlStr) // add the generated html string to the <form> element 
    $('#employeePositions').modal(); 
} 
+0

感謝ü您的快速性反應。 – Christeen