简体   繁体   English

使用 JQuery 从表中获取选定值

[英]Get Selected Value from Table using JQuery

I am using below code for getting check value from table which is printing all checked checkbox value.But i want to get only selected checkbox value not all checked value.我正在使用下面的代码从打印所有选中复选框值的表中获取检查值。但我只想获取选中的复选框值而不是所有选中值。

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()  

}


For adding table row if i click checkbox using below code which is working.Can anyone please help how to delete rows as well when i unselect checkboxes.如果我使用下面的代码单击复选框,则添加表格行。当我取消选择复选框时,任何人都可以帮助如何删除行。

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 HTML

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

Table桌子


        <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>           


CheckBox复选框

         <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>

You are actually getting all checkbox's values and pushing those in arrays by using '$.each'您实际上正在获取所有复选框的值并通过使用'$.each'将这些值推送到 arrays

Anyway, for the first part of your question, your checkbox should have a class eg chkbox , then you can get it's value in it's click handler:无论如何,对于你问题的第一部分,你的复选框应该有一个 class 例如chkbox ,然后你可以在它的点击处理程序中获得它的价值:

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

To delete a row from table, assign id to each tr once you create it:要从表中删除一行,请在创建每个tr后为其分配 id:

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

Then once checkbox is unselected, you can remove row in checkbox's click handler:然后,一旦取消选中复选框,您就可以删除复选框的单击处理程序中的行:

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

    }
 });

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM