多个复选框删除javascript

时间:2015-12-28 12:01:13

标签: javascript

我有来自数据库行的delete代码。但是当按deletecheckbox无法正常工作时,它会逐个删除。我想通过选中多次删除复选框

function confirmDelete(delUrl) {
        if (confirm("Are you sure you want to delete")) {
            document.location = delUrl;
        }
    }

function check() {
            document.getElementById("myCheck").checked = true;
        }

        function uncheck() {
            document.getElementById("myCheck").checked = false;
        }

HTML

<a href="javascript:confirmDelete('?action=deleteurl&id=<?php echo $row[0]; ?>')">
    <input type="checkbox" id="myCheck[]">delete</td>

PHP

<?php
if($_GET['action'] == "deletelink" && !empty($_GET['id']) && is_numeric($_GET['id'])) {
    $result = mysql_query("SELECT * FROM books WHERE id='".intval($_GET['id']);

    while ($row = mysql_fetch_array ($result) )
        unlink("/home/me/public_html/upload/image/{$row['image']}");
}

2 个答案:

答案 0 :(得分:0)

您的逻辑不适用于多次删除

这应该是html

<input type="checkbox" id="myCheck[]" class="multicheck" name="<?php echo $row[0]; ?>" value="<?php echo $row[0]; ?>"><a href="javascript:confirmDelete('?action=deleteurl&id=<?php echo $row[0]; ?>')">delete</a></td>


<a href="javascript:confirmMultiDelete(this)">Multiple Delete</a></td>

这应该是javascript

function confirmMultiDelete() {
    if (confirm("Are you sure you want to delete")) {
    var str = [];
    $.each(".multicheck:checked",function(){
    str.push($(this).val());
    });

    ids = str.join(",");
        document.location = '?action=deleteurl&id='+ids;
    }
}

答案 1 :(得分:0)

这是我的方法。

您需要进行一些修改才能进行多次删除。

  • 首先,您必须在checkbox中设置值,以便获得其值 删除。
  • 其次,您必须添加另一个按钮或操作才能删除多个 记录。

您的代码(带修改):

HTML + PHP代码

<a href="javascript:confirmDelete('?action=deleteurl&id=value="<?php echo $row[0]; ?>"')">delete
</a>
<input type="checkbox" class="delCheck" id="myCheck[]" value="<?php echo $row[0]; ?>">
</td>

<!-- This will generate html code like below -->
<!--
<td>
    <a href="javascript:confirmDelete('?action=deleteurl&id=1">delete</a>
        <input type="checkbox" class="delCheck" value="1" id="myCheck[]" class="delCheck">
    </td>
    <td><a href="javascript:confirmDelete('?action=deleteurl&id=2">delete</a>
        <input type="checkbox" class="delCheck" value="2" id="myCheck[]" class="delCheck">
    </td>
    <td><a href="javascript:confirmDelete('?action=deleteurl&id=3">delete</a>
        <input type="checkbox" class="delCheck" value="3" id="myCheck[]" class="delCheck">
    </td>
    <td><a href="javascript:confirmDelete('?action=deleteurl&id=4">delete</a>
        <input type="checkbox" class="delCheck" value="4" id="myCheck[]" class="delCheck">
    </td>
    <td>
        <input type="button" value="Delete" name="delete_value" id="delete_value" class="delCheck">
    </td>

-->

<!-- Multiple Delete Button -->
<input type="button" id="delete_value" name="delete_value" value="Delete" />

Javascript + Jquery

function confirmDelete(delUrl) {
    if (confirm("Are you sure you want to delete")) {
        document.location = delUrl;
    }
}

$( document ).ready(function() {            

    $("#delete_value").on("click", function (e){                    
        var ids = '';
        $('.delCheck:checked').each(function(i){           
            ids += $(this).val() + ','; 
        }); 
        // go to delete url with action delete_all_ur 
        confirmDelete('?action=delete_all_url&id='+ids)             
    });      
}); 

PHP删除代码:

if( @$_GET['action'] == "delete_all_url" ) 
    {

        $all_ids = $_GET['id']; 
        // Remove trailing ',' from IDs 
        $all_ids = trim($all_ids, ",");
        // Temporary variable to check only integer value passes
        $tempId = str_replace(",", "", $all_ids);

        // Check id numeric or not 
        if ( is_numeric($tempId) ) {            
            $sql_query = "SELECT * FROM books WHERE id in (".intval($_GET['id'].")";
            $result = mysql_query($sql_query);
            while ($row = mysql_fetch_array ($result) )
                unlink("/home/me/public_html/upload/image/{$row['image']}");            
        } else {
            //echo 'errors';
        }

    }