给表行赋值并在jquery中删除它

时间:2013-12-02 19:56:48

标签: javascript jquery html html5

我想为HTML中的每一行提供一个id,当我在textbox中输入该值时,我希望jquery删除该行。

我已经建立了这个小提琴

http://jsfiddle.net/vYAq9/3/

$(document).ready(function(){

  $("#addbutton").click(function(){
     var uniqueid = $("#textbox1").val();
     var text = $("#textbox2").val();
     $("<tr><td>row content</td></tr>").val(uniqueid).html(text).appendTo("#table1");
  });

  $("#removebutton").click(function(){
      var uniqueid = $("#textbox1").val();
     //I want to find row with uniqueid, and remove it from the table
  });

});

2 个答案:

答案 0 :(得分:1)

$(document).ready(function () {

    $("#addbutton").click(function () {
        var uniqueid = $("#textbox1").val();
        var text = $("#textbox2").val();
        $("<tr id=" + uniqueid + "><td>row content</td> </tr>").html(text).appendTo("#table1"); 
        // instead of value add an id | tr does not have a value!
    });

    $("#removebutton").click(function () {
        var uniqueid = $("#textbox1").val(); 
        $('#' + uniqueid).remove(); // use id to delete row with the same id
    });

});

FIDDLE

答案 1 :(得分:0)

<强> JSFIDDLE DEMO

  $("#removebutton").click(function(){
      var uniqueid = $("#textbox1").val();
      $('#' + uniqueid).remove();
     //I want to find row with uniqueid, and remove from table
  });