使用jquery删除表行

时间:2017-03-24 09:34:07

标签: javascript jquery html

我正在使用它添加一行:

function myFunction4() {
  var table = document.getElementById("myTable");
  var row = table.insertRow(0);
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1); 
  var produs = $( "#produs4" ).val(); 
  var cantitate = $(".cantitate4").val(); 
  var cell3 = row.insertCell(2);
  cell1.innerHTML = produs;
  cell2.innerHTML = cantitate; 
  cell3.innerHTML = " <input type='button' value='X' onclick='SomeDeleteRowFunction(this)'>"
  $(".overlay_cantitate4").fadeOut(500)
}

然后我尝试将其删除但不起作用

function SomeDeleteRowFunction() {
  var p = o.parentNode.parentNode;
  p.parentNode.removeChild(p);
}

变量

<select id="produs4"><option selected="selected"> Prajitura cu mar </option></select> 
             <center> <input class="cantitate4 cantitate" type="text" value="1"> 

4 个答案:

答案 0 :(得分:1)

在jQuery中,向上查找row标记,然后将其删除。

<script>
function SomeDeleteRowFunction() {
  $(this).parents('tr').remove();
}
</script>

答案 1 :(得分:0)

您可以在删除时更改删除此功能的删除功能


    function SomeDeleteRowFunction(element) {
      var p = element.parentNode.parentNode;
      p.parentNode.removeChild(p);
    }

答案 2 :(得分:0)

由于您正在使用jQuery,因此最好在您的JS代码中附加click事件,

首先给目标一个类:

['attribute' => 'vendor_id', 'format'=>'raw', 'value' => function($model){
                                return $model->addedUser->company_name;
                        }],

然后附加点击事件:

passport.use('facebook', new FacebookStrategy({
  clientID        : secret.facebook.clientID,
  clientSecret    : secret.facebook.clientSecret,
  callbackURL     : secret.facebook.callback
},

注意:我们应该在此处使用事件委派 .on() ,因为这些元素是动态生成的。

希望这有帮助。

答案 3 :(得分:0)

您可以使用.closest,也可以使用普通的javascript: https://developer.mozilla.org/en-US/docs/Web/API/Element/closest

请注意,IE和Edge不支持此功能,但可以使用polyfill。

同样在jQuery中使用.closest()方法会更好,因为它只找到一个最接近的元素而不是节点父节点中所有匹配的元素:

来自:https://api.jquery.com/parents/

  

匹配从直接父级订购的元素;元素按照从最近的父元素到外元元素的顺序返回。

相关问题