在Javascript中动态删除按钮上的选定行

在Javascript中动态删除按钮上的选定行,javascript,jquery,Javascript,Jquery,我编写了这样的代码,当用户输入名称和类型并按下“添加”按钮时,它会将名称、类型、可用性和删除按钮添加到列表中。我希望当按下删除按钮时,相应的行被删除 我的JavaScript: function AddToWishlist2() { var table = document.getElementById("WishListTable"); nameInput2 = document.getElementById("API2").value; search2(); radiobutton1 = d

我编写了这样的代码,当用户输入名称和类型并按下“添加”按钮时,它会将名称、类型、可用性和删除按钮添加到列表中。我希望当按下删除按钮时,相应的行被删除

我的JavaScript:

function AddToWishlist2() {
var table = document.getElementById("WishListTable");
nameInput2 = document.getElementById("API2").value;
search2();
radiobutton1 = document.getElementById("radio-choice-1-1").value;
radiobutton2 = document.getElementById("radio-choice-2-2").value;
// this sets the value to TV show or Movie

if (document.getElementById("radio-choice-1-1").checked == true) {
    radiobuttonfinal = radiobutton1;
}
else if (document.getElementById("radio-choice-2-2").checked == true) {
    radiobuttonfinal = radiobutton2;
}
//setting the value of radio button to a single switching variable
var row = table.insertRow(1);
var cell4 = row.insertCell(0);
var cell1 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell2 = row.insertCell(2);

if (nameInput2 != ""  && radiobuttonfinal != 0 ) {
    cell4.innerHTML = '<input id="Button" type="button" value="Delete" />';
    // JUST A BULLET HERE WITH AN ONCLICK FUNCTION THAT REMOVES THE ROW WITH var Delete = row.delete(0);
    //will have a bullet point
    cell1.innerHTML = nameInput2;
    //Title prints user input of title
    cell2.innerHTML = radiobuttonfinal;
    //Type prints user input of type
    cell3.innerHTML = GetStatus2();
}
//if there is an entry in title and a radio button checked, it pushes it
alert(nameInput2 + " has been added to your WishList\n\nClick View My WishList to view your WishList");   
//if there is an entry in title and a radio button checked, it pushes it
}
//Page two function checks questions and adds to wishlist
函数AddToWishlist2(){
var table=document.getElementById(“WishListTable”);
nameInput2=document.getElementById(“API2”).value;
search2();
radiobutton1=document.getElementById(“radio-choice-1-1”)。值;
radiobutton2=document.getElementById(“radio-choice-2-2”)。值;
//这会将值设置为电视节目或电影
if(document.getElementById(“radio-choice-1-1”).checked==true){
radiobuttonfinal=radiobutton1;
}
else if(document.getElementById(“radio-choice-2-2”).checked==true){
radiobuttonfinal=radiobutton2;
}
//将单选按钮的值设置为单个开关变量
var行=table.insertRow(1);
var cell4=行插入单元格(0);
var cell1=行插入单元格(1);
var cell3=行插入单元格(2);
var cell2=行插入单元格(2);
如果(名称输入2!=“”&&radiobuttonfinal!=0){
cell4.innerHTML='';
//这里只有一个带有ONCLICK函数的项目符号,该函数使用var Delete=ROW.Delete(0)删除行;
//会有一个要点
cell1.innerHTML=nameInput2;
//标题打印用户输入的标题
cell2.innerHTML=radiobuttonfinal;
//类型打印类型为的用户输入
cell3.innerHTML=GetStatus2();
}
//如果标题中有一个条目并且选中了一个单选按钮,它就会按下它
警报(nameInput2+“已添加到您的愿望列表\n\n单击查看我的愿望列表以查看您的愿望列表”);
//如果标题中有一个条目并且选中了一个单选按钮,它就会按下它
}
//第二页功能检查问题并添加到愿望列表
我的HTML:

<div data-role="page" id="pagetwo">
    <div> <center>
        <h2> My WishList </h2>
    </div>
    <div>
        <table align="center" style= "width:100%" id="WishListTable"> 
            <tr>
                <td> </td>
                <td> Title </td>
                <td> Type </td>
                <td> Status </td>
            </tr>
            <tr>
                <td></td>
            </tr>
        </table>
<center> <h5> Title: </h5>
    <input type="text" name="WishListSearch" id="API2" class = "APIClass2">
    <fieldset data-role="tvmshowtype">
    <legend style = "text-align:center"> <h4> Type: </h4> </legend>
    <input type="radio" name="radio-choice" id="radio-choice-1-1" value="TV Show"/>
    <label for="radio-choice-1-1" style = "text-align:center">TV Show</label>
    <input type="radio" name="radio-choice" id="radio-choice-2-2" value="Movie"/>
    <label for="radio-choice-2-2" style = "text-align:center">Movie</label>
    </fieldset>
        <button onclick="AddToWishlist2()"> Add to My WishList</button>

我的愿望清单
标题
类型
地位
标题:
类型:
电视节目
电影
添加到我的愿望列表中

  • 将类
    删除行
    添加到删除按钮:

    cell4.innerHTML = '<input id="Button" type="button" value="Delete" class="delete-row"/>';
    
  • 完整代码:

    AddToWishlist2=函数(){
    var table=document.getElementById(“WishListTable”);
    nameInput2=document.getElementById(“API2”).value;
    //search2();
    radiobutton1=document.getElementById(“radio-choice-1-1”)。值;
    radiobutton2=document.getElementById(“radio-choice-2-2”)。值;
    //这会将值设置为电视节目或电影
    if(document.getElementById(“radio-choice-1-1”).checked==true){
    radiobuttonfinal=radiobutton1;
    }
    else if(document.getElementById(“radio-choice-2-2”).checked==true){
    radiobuttonfinal=radiobutton2;
    }
    //将单选按钮的值设置为单个开关变量
    var行=table.insertRow(1);
    var cell4=行插入单元格(0);
    var cell1=行插入单元格(1);
    var cell3=行插入单元格(2);
    var cell2=行插入单元格(2);
    如果(名称输入2!=“”&&radiobuttonfinal!=0){
    cell4.innerHTML='';
    //这里只有一个带有ONCLICK函数的项目符号,该函数使用var Delete=ROW.Delete(0)删除行;
    //会有一个要点
    cell1.innerHTML=nameInput2;
    //标题打印用户输入的标题
    cell2.innerHTML=radiobuttonfinal;
    //类型打印类型为的用户输入
    cell3.innerHTML=GetStatus2();
    }
    //如果标题中有一个条目并且选中了一个单选按钮,它就会按下它
    警报(nameInput2+“已添加到您的愿望列表\n\n单击查看我的愿望列表以查看您的愿望列表”);
    //如果标题中有一个条目并且选中了一个单选按钮,它就会按下它
    }
    //第二页功能检查问题并添加到愿望列表
    $(文档)。在('单击','上。删除行',函数(){
    $(this.parents('tr').remove();
    });
    
    
    我的愿望清单
    标题
    类型
    地位
    标题:
    类型:
    电视节目
    电影
    
    添加到我的愿望列表
    事件委派。向表中添加一个事件处理程序,该处理程序将删除
    事件.target
    的parentNode(甚至parentnodes父节点),如果该目标是一个值为“delete”的按钮。谢谢!我遇到这些问题的原因是因为我使用的Ajax是1.5而不是2.1.1。它工作得很好
    $(document).on('click','.delete-row', function(){
         $(this).parents('tr').remove();
    });