簡體   English   中英

如何使用 ajax-jquery 使用 PHP-CodeIgniter 將選定的復選框數據設置為模態

[英]How can use ajax-jquery to get selected checkbox data to modal using PHP-CodeIgniter

如何在所有行中使用帶有復選框的 HTML 表,以及單擊按鈕時,我想將那些選定的復選框 ID 設置為模態。 所以我想使用 ajax 從數據庫中獲取所選 ID 的相關數據,並使用 PHP-CodeIgniter 以模式形式顯示在表格中。 有人可以幫我得到這個嗎?

我嘗試了很多方法,但都失敗了。

非常感謝。

請看我的代碼。

在我的 VIEW 頁表上

<table id="dynamic-table">
        <thead>
            <th> Check</th>
            <th> Name</th>
            <th> Country</th>
        </thead>
        <tbody>
            <tr>
                <td> <input type="checkbox" name="ch1" value="1"> </td>
                <td> Rashid </td>
                <td> India </td>
            </tr>
            <tr>
                <td> <input type="checkbox" name="ch1" value="2"> </td>
                <td> Nishad </td>
                <td> India </td>
            </tr>
            <tr>
                <td> <input type="checkbox" name="ch1" value="3"> </td>
                <td> sajeesh </td>
                <td> India </td>
            </tr>
        </tbody>

    </table>

    <button name="submit" id="subm"> Submit </button>
    <button name="cancel" id="canc"> Cancel </button>


    <div id="message"></div>

查看頁面上的腳本:

<script>
    $(document).ready(function(){
        $("#subm").click(function(){
            getValueUsingParentTag();
        });
    });
    function getValueUsingParentTag(){
var chkArray = [];

/* look for all checkboes that have a parent id called 'checkboxlist' attached to it and check if it was checked */
$("#dynamic-table input:checked").each(function() {
    chkArray.push($(this).val());
});

/* we join the array separated by the comma */
var selected;
selected = chkArray.join(',') ;




var url = "<?php echo base_url(); ?>/index.php/Ajax";
    $.post( url, { test_input: selected } ) 

         .done(function(data){
            $("#message").html(data.result);
        });





/* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
if(selected.length > 0){
    alert("You have select" + selected);    
}else{
    alert("Please at least check one of the checkbox"); 
}

}

我的 AJAX 控制器

<?php

定義('BASEPATH')或退出('不允許直接腳本訪問');

類 Ajax 擴展 CI_Controller {

public function index()
{
    $posted = $this->input->post('test_input');
    echo json_encode(array('result'=> $posted));        

}

}

請建議我編輯或一個好的。 實際上,我想從數據庫中檢索數據。 但首先在這段代碼中,我只是嘗試在沒有 db 的情況下使用 ajax。 一旦成功,我就可以繼續進行數據庫數據檢索。

嘗試以這種方式將 id 的值存儲在復選框的 value 屬性中。 然后將類“checkAll”添加到表格標題中的復選框中。 並將類“my-checkbox”添加到每行的復選框,然后以下方法將同時選擇/取消選擇所有復選框。 編寫另一個函數,該函數將使用類 my-checkbox 循環遍歷每個復選框,並將復選框的值存儲在數組中,然后返回數組。 希望這可以幫助....

// Select / Deselect all checkboxes
$(".checkAll").click(function () {
    $('input:checkbox').not(this).prop('checked', this.checked);
    console.log(getCheckedValues());
});

function getCheckedValues(){
    var checkboxes = document.getElementsByClassName('my-checkbox');
    var checkboxesChecked = [];
    // loop over them all
    for (var i=0; i < checkboxes.length; i++) {
      // And stick the value of checked ones onto an array...
      if (checkboxes[i].checked) {
        checkboxesChecked.push(checkboxes[i].value);
      }
    }
    // Return the array if it is non-empty, or null
    return checkboxesChecked.length > 0 ? checkboxesChecked : null;
  }

;

暫無
暫無

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

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