how to use splice to remove course from jquery code

how to use splice to remove course from jquery code

by Akram kamal

HTML

<form>
    <input type="hidden" value="@ViewBag.hid" id="hid" />



    <br /> Courses :
    <select id="CourseId">
        <option value="">---select---</option>
        <option value="1">c#</option>
        <option value="2">java</option>
        <option value="3">php</option>
        <option value="4">python</option>
    </select>
    <br />
    <br />
    <table id="tb"></table>
    <input type="submit" value="save" />

</form>

JavaScript

var index = 0;
 $("#CourseId").change(function() {

     var id = $(this).val();
     var txt = $("#CourseId option:selected").text();
     $("#tb").append("<tr><td><input type = 'hidden' name='empcourses[" + index + "].CourseId' value='" + id + "'/></td><td>" + txt + "</td><td><input type='button' value='remove' class='r'</td></tr>")

     index++;
 });
 $("#tb").on("click", ".r", function() {
     $(this).closest("tr").remove()
 });

 $("form").submit(function() {
     var values = $("#tb tr td:nth-child(2)").map(function() {
         return $(this).text()
     }).get()
     console.log(values)
     return false // for debugging!
     $.ajax({
         url: "/Employee/getcoursesbyempid",
         data: {
             x: values
         },
         success: function(res) {
             $.each(res, function(i, e) {
                 $("#tb").append("<tr><td><input type = 'hidden' name='empcourses[" + index + "].CourseId' value='" + e.Id + "'/></td><td>" + e.CourseName + "</td><td><input type='button' value='remove' class='r'</td></tr>")

                 index++;
             });
         }

     })


 })