使用jQuery获取当前选定复选框的值

时间:2019-01-25 05:57:22

标签: javascript jquery

我需要从表格的选定复选框中获取当前选定的值。下面我在用于从复选框中获取选定值的代码下面。但是它正在从表格中获取所有选定的值。我只需要选定的表格行值。 / p>

$.each($('input[type="checkbox"]:checked').closest("td").next("td"), function() {
  values.push($(this).text().trim())

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="DeviceTable" class="table table-bordered">
  <thead>
    <tr class="info">
      <th style="width: 10px;">
        <input type="checkbox" id="check_selectall-custom" onclick="selectAllCustom(this)" />SA</th>

      <th>Device</th>
      <th> Type</th>
      <th> Model</th>
      <th> Status</th>

    </tr>
  </thead>
  <tbody class="parameter_table">

    <tr id="tr_device_id1">
      <td>
        <input type="checkbox" name="checkBox[]" onclick="DeviceClickedBox()">
      </td>

      <td id="macadd" style="word-break:break-all;">
        Mac1
      </td>

      <td style="word-break:break-all;">
        Dev 1
      </td>
      <td style="word-break:break-all;">
        Model 1
      </td>
      <td class="Selected_Device" id="Selected_Device">
        <b id="Selected_Device" style="float: right;"></b>

      </td>
    </tr>
    <tr id="tr_device_id2">
      <td>
        <input type="checkbox" name="checkBox[]" onclick="DeviceClickedBox()">
      </td>

      <td id="macadd" style="word-break:break-all;">
        Mac 2
      </td>

      <td style="word-break:break-all;">
        Parm 2
      </td>
      <td style="word-break:break-all;">
        Model 2
      </td>
      <td class="Selected_Device" id="Selected_Device">
        <b id="Selected_Device" style="float: right;"></b>

      </td>
    </tr>
  </tbody>
</table>

2 个答案:

答案 0 :(得分:0)

给TD一个类而不是ID

我不知道您要单击什么,但是在这里我只需单击复选框即可

$(function() { // assuming jquery is loaded, execute on page load
  $("#check_selectall-custom").on("click", function() {
    var checked = this.checked;
    $("input[type=checkbox]").not(this).prop("checked",checked);
  });
  $("#getval").on("click", function() {
    var values = $("input[type=checkbox]:checked").not(this).map(function() {
      return $(this).closest("tr").find(".macadd").text().trim();
    }).get();
    $("#res").html(values.join("<br/>"))
  });
});
#tr_device_id1 td {
  word-break: break-all
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="DeviceTable" class="table table-bordered">
  <thead>
    <tr class="info">
      <th style="width: 10px;">
        <label><input type="checkbox" id="check_selectall-custom" />SA</label>
      </th>

      <th>Device</th>
      <th> Type</th>
      <th> Model</th>
      <th> Status</th>

    </tr>
  </thead>
  <tbody class="parameter_table">

    <tr id="tr_device_id1">
      <td>
        <input type="checkbox" name="checkBox[]">
      </td>

      <td class="macadd">
        Mac 1
      </td>

      <td class="device">
        Dev 1
      </td>
      <td class="model">
        Model 1
      </td>
      <td class="Selected_Device">
      </td>
    </tr>
    <tr id="tr_device_id2">
      <td>
        <input type="checkbox" name="checkBox[]">
      </td>

      <td class="macadd">
        Mac 2
      </td>

      <td class="device">
        Dev 2
      </td>
      <td class="model">
        Model 2
      </td>
      <td class="Selected_Device">
      </td>
    </tr>
  </tbody>
</table>
<button type="button" id="getval">Show checked</button>
<span id="res"></span>

答案 1 :(得分:-2)

def connect_gsheet(gsheet_key):

    path = '/home/secrets/credentials/'
    cjson = open(path + "sheets_connect.json", 'r', encoding = 'utf8')
    cjson = json.load(cjson)
    print(cjson['client_email'])
    try:
        gc = pygsheets.authorize(service_file= path + "sheets_connect.json", no_cache=True, outh_nonlocal = True)
        ss = gc.open_by_key(gsheet_key)
        print("Spreadsheet Connected!!! ")
        return ss
    except Exception as err:
        print(err)
        pass
$number = 1234.56;

number_format($number, 2, ',', ' '); // 1 234,56