0
<a class="checkModelButton" href="check.php">Check</a>
<div id="model_list"></div>

包括 jquery 和函数 deleteRow():

jQuery('.checkModelButton').click(function(event){
   event.preventDefault();
   var url = jQuery(this).attr('href');
   jQuery.ajax({
         type: 'get',
         cache: false,
         url: url,
         success: function(html){
             jQuery('#model_list').html(html);
         }
   });
});
function deleteRow(id) {
    try {
        var table = document.getElementById('model_list');
        var rowCount = table.rows.length;
        for(var i=0; i<rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null != chkbox && true == chkbox.checked) {
                table.deleteRow(i);
                rowCount--;
                i--;
            }
        } 
        jQuery("input[type=checkbox]:checked").each(function() { 
            jQuery(this).parents("tr").remove(); 
        });    

    } catch(e) {
        alert(e);
    }
}

在 check.php 中返回 html 是:

<input type="button" value="Delete Row" onclick="deleteRow('model_list')" />
<table id="model_list">
   <thead>
      <tr>
        <th>#</th>
        <th>Name</th>
      </tr>
   </thead> 
   <tbody>
      <tr>
        <td><input type="checkbox" value="1" name="model_id[]" class="item"></td>
        <td>Nokia N71</td>
      </tr>
   </tbody>
</table>

加载ajax后,我检查了表单输入并单击按钮Delete Row,但是错误无法删除该行并且错误是alert(Table model_list is empty),如何解决?

4

1 回答 1

2

jQuery 确实为我们简化了选择过程,并且还提供了许多 JavaScript 在没有 try/catch 块的情况下无法提供的故障保护。由于您已经在使用 jQuery,您可以通过执行以下操作真正简化您的 deleteRow() 函数:

function deleteRow(id) {   // the id variable is unnecessary and can be removed
    // Grab all the rows in the table (the > sign targets the elements directly inside the current one (not cascading)
    var rows = jQuery("#model_list > tbody > tr");
    // Iterate through the rows
    jQuery(rows).each(function(key, value) {
        // Look inside each row for a checked checkbox
        if (jQuery(this).find("input:checkbox[checked='checked']").length > 0) {
            // If one is found, then remove the whole row (jQuery(this) refers to the current row
            jQuery(this).remove();
        }
    });
}

为了使上面的示例正常工作,我在同一个文件中创建了一个临时表。由于您正在使用数据动态加载表行,因此它的功能应该类似于下面的静态示例:

<input type="button" value="Delete Row" onclick="deleteRow('model_list')" />
<table id="model_list">
    <thead>
        <tr>
            <th>#</th>
            <th>Name</th>
        </tr>
    </thead> 
    <tbody>
        <tr>
            <td><input type="checkbox" value="1" name="model_id[]" class="item"></td>
            <td>Nokia N71</td>
        </tr>
        <tr>
            <td><input type="checkbox" value="2" name="model_id[]" class="item"></td>
            <td>Nokia N72</td>
        </tr>
        <tr>
            <td><input type="checkbox" value="3" name="model_id[]" class="item"></td>
            <td>Nokia N73</td>
        </tr>
    </tbody>
</table>

请让我知道这是否有帮助或如果您有任何其他问题。:)

于 2012-07-28T04:23:59.703 回答