使用jQuery根据Checkbox值在表行中追加数据

时间:2019-01-24 06:30:11

标签: javascript jquery ruby-on-rails

我必须更新要从html表中选择(复选框)的mac设备的状态。如果我要从html表中选择一个mac设备,它将显示mac的脱机或联机状态。下面是我为此使用的代码问题是如果我选择的是仅Mac的第一行状态列正在更新。我想更新相应列中的选定Mac行状态。

我正在尝试下面的代码,但是它不起作用

var nextColumnCell = $(selected_device_mac).parent("tr").find(selected_device_id)

if(result.includes("On-line")){
    nextColumnCell.html("");
    nextColumnCell.append(result + "  ✅")

}else if(result.includes("Off-line")){
    $(selected_device_id).html("");
    $(selected_device_id).append(result+"  ❗");
}

实际代码

function macDeviceClickedBox(checkBoxName){

    var values = new Array();
    var selected_device_id ;
    var selected_device_mac ;
    $.each($("input[name='"+checkBoxName+"']:checked").closest("td").next("td"), function () {
        values.push($(this).text().trim())

    });
    document.getElementById("macaddress").value = values.join("\n");
    selected_device_id = '#Selected_Device';
    selected_device_mac = values.join("\n");
    alert(selected_device_mac)
    $('#cover-spin').show();

    $.ajax({

        method: "GET",
        dataType: "text",
        url: "getDeviceType",
        data: {
            mac : selected_device_mac.trim(),
        },

        success: function(result) {
            $('#cover-spin').hide();
            if (result !== "") {
                if(result.includes("On-line")){
                    $(selected_device_id).html("");
                    $(selected_device_id).append(result+"  ✅");
                }else if(result.includes("Off-line")){
                    $(selected_device_id).html("");
                    $(selected_device_id).append(result+"  ❗");
                }

            }else{
                $(selected_device_id).html("");
                $(selected_device_id).append("Not Selected   ❗");
            }
        }

    });
}

Html.erb

<table id="DeviceTable" class="table table-bordered">
    <thead>
        <tr class="info">
            <th style="width: 10px;">Select</th>
            <th>Device</th>
            <th> Type</th>
            <th> Model</th>
            <th> Status</th>
        </tr>
    </thead>
    <tbody class="parameter_table">
        <% @devices.all.each do |parameter| %>
        <tr id="device_<%=parameter.id %>">
            <td >
                <input  type="checkbox" class="checkBox" name="checkBox[]" onchange="macDeviceClickedBox(this.name)">
            </td>
            <td  id="macaddress" style="word-break:break-all;">
                <%= parameter.mac_address%>
            </td>
            <td   style="word-break:break-all;">
                <%= parameter.device_type%>
            </td>
            <td   style="word-break:break-all;">
                <%= parameter.model%>
            </td>
            <td class="Selected_Device" id="Selected_Device">
                <b id="Selected_Device" style="float: right;">!Not Selected</b>
            </td>
        </tr>
        <% end %>
    </tbody>
</table>

0 个答案:

没有答案