如何使用javascript删除表中的特定行? [英] How to delete a specific row in a table using javascript?

查看:65
本文介绍了如何使用javascript删除表中的特定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止我已经实现了:

what i have implemented so far:


  1. 在输入字段中输入值,然后单击添加按钮,输入的值得到添加到新行。

  2. 当我单击删除按钮时,所有行都将被删除。

我需要实现的目标:


  1. 复选框应该添加到每一行。

  2. 如果我选中复选框并单击删除按钮,则只有该特定行应该被删除,如果我也选择了多个复选框,它应该可以工作。
    3.单击添加按钮后,清除输入字段。
    任何人都可以查看这个并告诉我该怎么做。

//Javascript code to Add new rows onclick of a button and to delete row .
function addMoreRows() {

    var user = document.getElementById('user_id').value;
    var date = document.getElementById('date').value;
    var color = document.getElementById('color').value;
    var table = document.getElementById('tbl_id');

    var row = table.insertRow();

    var userName = row.insertCell(0);
    var Date = row.insertCell(1);
    var Color = row.insertCell(2);
    var checkbox = row.insertCell(3);

    userName.innerHTML = user;
    Date.innerHTML = date;
    Color.innerHTML = color;

}

function deleteMoreRows(tableID) {

    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;

    for (var i = 0; i < rowCount; i++) {

        table.deleteRow(i);
        rowCount--;
        i--;
    }
}

<!-- HTML markup for the input fields and table . -->
<form align="center" method="GET">
   Enter your name : <input type="text" name="users" id="user_id"  value="name" onfocus="if(this.value == 'name') {this.value=''}" onblur="if(this.value == ''){this.value ='name'}"><br>
   Select the Date : <input type="date"  id="date"><br>
   Select your favorite color: 
   <select id="color" required>
      <option value="yellow">yellow</option>
      <option value="red">red</option>
   </select>
   <br>
   <br>
   <input type="button" id="mysubmit" value="Add Row" onClick="addMoreRows()">
   <input type="button" id="delete" value="Delete" onClick="deleteMoreRows('tbl_id')">
</form>
<br>
<br>
<table id="tbl_id" style="text-align:center" align="center" valign="top">
   <thead>
      <tr>
         <th style="width:200px;">Name</th>
         <th style="width:200px;">Date</th>
         <th style="width:200px;">Color</th>
      </tr>
   </thead>

推荐答案

让我知道这是否适合你:

Let me know if this works for you:

//Javascript code to Add new rows onclick of a button and to delete row .

var rowId = 0;

function addMoreRows() {

    var user = document.getElementById('user_id').value;
    var date = document.getElementById('date').value;
    var color = document.getElementById('color').value;
    var table = document.getElementById('tbl_id');

    var row = table.insertRow();

    var rowBox = row.insertCell(0);
    var userName = row.insertCell(1);
    var Date = row.insertCell(2);
    var Color = row.insertCell(3);
    var checkbox = row.insertCell(4);

    rowBox.innerHTML = '<input type="checkbox" id="delete' + getRowId() + '">';
    userName.innerHTML = user;
    Date.innerHTML = date;
    Color.innerHTML = color;

}

function deleteMoreRows(tableID) {

    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    var selectedRows = getCheckedBoxes();

    selectedRows.forEach(function(currentValue) {
      deleteRowByCheckboxId(currentValue.id);
    });
}

function getRowId() {
  rowId += 1;
  return rowId;
}

function getRowIdsFromElements($array) {
  var arrIds = [];

  $array.forEach(function(currentValue, index, array){
    arrIds.push(getRowIdFromElement(currentValue));
  });

  return arrIds;
}

function getRowIdFromElement($el) {
    return $el.id.split('delete')[1];
}

//ref: http://stackoverflow.com/questions/8563240/how-to-get-all-checked-checkboxes
function getCheckedBoxes() {
  var inputs = document.getElementsByTagName("input");
  var checkboxesChecked = [];

  // loop over them all
  for (var i=0; i < inputs.length; i++) {
     // And stick the checked ones onto an array...
     if (inputs[i].checked) {
        checkboxesChecked.push(inputs[i]);
     }
  }

  // Return the array if it is non-empty, or null
  return checkboxesChecked.length > 0 ? checkboxesChecked : null;
}

//ref: http://stackoverflow.com/questions/4967223/delete-a-row-from-a-table-by-id
function deleteRowByCheckboxId(CheckboxId) {
    var checkbox = document.getElementById(CheckboxId);
    var row = checkbox.parentNode.parentNode;                //box, cell, row, table
    var table = row.parentNode;

    while ( table && table.tagName != 'TABLE' )
        table = table.parentNode;
    if (!table) return;
    table.deleteRow(row.rowIndex);
}

<!-- HTML markup for the input fields and table . -->
<form align="center" method="GET">
   Enter your name : <input type="text" name="users" id="user_id"  value="name" onfocus="if(this.value == 'name') {this.value=''}" onblur="if(this.value == ''){this.value ='name'}"><br>
   Select the Date : <input type="date"  id="date"><br>
   Select your favorite color: 
   <select id="color" required>
      <option value="yellow">yellow</option>
      <option value="red">red</option>
   </select>
   <br>
   <br>
   <input type="button" id="mysubmit" value="Add Row" onClick="addMoreRows()">
   <input type="button" id="delete" value="Delete" onClick="deleteMoreRows('tbl_id')">
</form>
<br>
<br>
<table id="tbl_id" style="text-align:center" align="center" valign="top">
   <thead>
      <tr>
         <th style="width:200px;">Delete</th>
         <th style="width:200px;">Name</th>
         <th style="width:200px;">Date</th>
         <th style="width:200px;">Color</th>
      </tr>
   </thead>

这篇关于如何使用javascript删除表中的特定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆