【问题标题】:Get Selected Value from Table using JQuery使用 JQuery 从表中获取选定的值
【发布时间】:2020-06-01 15:41:33
【问题描述】:

我正在使用下面的代码从打印所有选中复选框值的表中获取检查值。但我只想获取选中的复选框值而不是所有选中值。

function clickedBoxAdd(checkBoxvalue) {
    var values = new Array();
    $.each($("input[name='" + checkBoxvalue + "']:checked").closest("td").next("td").next("td"),
           function () {
                values.push($(this).text());
           });
           alert(values);   
           addVariablesRow1()  

}


如果我使用下面的代码单击复选框,则添加表格行。当我取消选择复选框时,任何人都可以帮助如何删除行。

function addVariablesRow1() {
            var $t = $("#addTable");
            var $row = $($t.find("tr")[1]);
            var r = $row.clone();
            r.find("input[type=text]").val("")
            r.find("input[type=checkbox]").attr('checked',  false)
            r[0].style.display = "";
            r.appendTo($t);
}

HTML

 <input type="checkbox" class="checkBox" name="checkBox_B[]" onclick="clickedBoxAdd(this.name)">

表格


        <table id="addTable" class="table table-bordered">
        <thead>
                <tbody class="snmp_oid_table">

        <tr class="info">
          <th>Oid</th>
          <th>Data Type</th>
          <th>Set Value</th>
        </tr>
        </thead>
  <tr style="display:none">

            <td>
              <input type="text" class="name" id="oids" name="oid">
            </td>   

            <td>
              <input type="text" class="text" name="oid_description">
            </td>
            <td>
              <input type="text" class="expectedValue" name="expected_value">
            </td>
</tr>
          </tbody>
      </table>           


复选框

         <table id="addOidTable" class="table table-bordered">
           <thead>
         <tr class="info">
           <th style="width: 10px;">
           <input type="checkbox" id="checkBox_add_b[]" onclick="selectAddAll(this)"/>SA</th>
           <th>name</th>
           <th>Oid </th>
         </tr>
       </thead>
         <tbody class="oid_table">
             <% @all_b_oids.each do |oids| %> 
                <tr id="tr_snmp_<%=oids['id'] %>">
                   <td>
                     <input type="checkbox" class="checkBox" name="checkBox_Oid_B[]" onclick="clickedBoxAddAll(this.name)">
                   </td>
                   <td style="word-break:break-all;">
                      <%= oids['name']%>   
                    </td>
                     <td style="word-break:break-all;">
                         <%= oids['oid']%>    
                     </td>
                 </tr>
                 <% end %>
           </tbody>
         </table>

【问题讨论】:

  • selected checkbox value 是什么意思?最后检查的值?如果我们有最少的可重复示例来理解您的问题,那也很棒
  • 是的,Kenny。我只需要最后检查的值。
  • 在这类问题中,一个最小可重现的例子对理解问题很有帮助。因为我们不能在黑暗中发射飞镖。

标签: javascript jquery ruby-on-rails-4


【解决方案1】:

您实际上是在获取所有复选框的值并使用 '$.each' 将它们推送到数组中

无论如何,对于您问题的第一部分,您的复选框应该有一个类,例如chkbox,那么你可以在它的点击处理程序中获取它的值:

 $(".chkbox").click(function() {
$(this).closest('td').html()
});

要从表中删除一行,请在创建后为每个 tr 分配 id:

r.attr("id", "rowid");

然后,一旦取消选中复选框,您就可以在复选框的点击处理程序中删除行:

 $(".chkbox").click(function() {
    if(!$(this).is(':checked')) {
       $('#rowid').remove();

    }
 });

希望这会有所帮助。

【讨论】:

  • thanks.$(this).val(); is not working.I'm getting value as "on".But table value is "Testing"
  • 这实际上是文本而不是值,为此尝试$(this).next('label').text();。也请粘贴您的复选框和表格 html 以便更好地理解。
  • 请更新问题以粘贴复选框的完整输入标签
  • 已添加。请检查
  • 在` `我看不到任何结束标签和文字
猜你喜欢
  • 2013-06-14
  • 1970-01-01
  • 2012-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多