加载 ajax jquery 后使用函数 deleteRow 时出错


Error when using function deleteRow after load ajax jquery?

<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);
    }
}

在检查中.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>

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

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>

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