简体   繁体   English

jQuery,如何获取表行中特定复选框的值?

[英]jQuery, how to get value of a specific checkbox in table row?

I have table with multiple rows but its row cotain 3 checkbox and button. 我有多行的表,但它的行包含3复选框和按钮。 Now, I want to update db table with respect to this button and the check box. 现在,我要针对此按钮和复选框更新数据库表。 But, the problem is that I am not able to get the value of the check box when I clicked on each rows button. 但是,问题是,当我单击每行按钮时,我无法获得复选框的值。 Please find the below code and let me know how can I get the value as per button on each row. 请找到以下代码,让我知道如何获取每一行上每个按钮的值。

<table width="100%">
  <tr class="tbrow">
    <td width="12%">
      <span class="name">test.html</span>
    </td>
    <input type="hidden" value="0" class="file_id" />
    <td width="5%">
      <span class="action">
        <input type="checkbox" value="1" class="read" title="read" /> Read
      </span>
    </td>
    <td width="5%">
      <span class="action">
        <input type="checkbox" value="1" class="write" title="write" /> Write
      </span>
    </td>
    <td width="7%">
      <span class="action">
        <input type="checkbox" value="1" class="download" title="download" /> Download
      </span>
    </td>
    <td width="5%">
      <span class="action">
        <input type="checkbox" value="1" class="share" title="share" /> Share
      </span>
    </td>
    <td width="80%">
      <span class="action">
        <input type="submit" name="assign" class="assign" title="Submit" value="Submit" />
      </span>
    </td>
  </tr>
  <tr class="tbrow">
    <td width="12%">
      <span class="name">testing.doc</span>
    </td>
    <input type="hidden" value="1" class="file_id" />
    <td width="5%">
      <span class="action">
        <input type="checkbox" value="1" class="read" title="read" /> Read
      </span>
    </td>
    <td width="5%">
      <span class="action">
        <input type="checkbox" value="1" class="write" title="write" /> Write
      </span>
    </td>
    <td width="7%">
      <span class="action">
        <input type="checkbox" value="1" class="download" title="download" /> Download
      </span>
    </td>
    <td width="5%">
      <span class="action">
        <input type="checkbox" value="1" class="share" title="share" /> Share
      </span>
    </td>
    <td width="80%">
      <span class="action">
        <input type="submit" name="assign" class="assign" title="Submit" value="Submit" />
      </span>
    </td>
  </tr>
</table>

Now, When I click on read check box of first row and click submit, it should pickup this value along with the hidden value of class="file_id" value. 现在,当我单击第一行的“读取”复选框并单击“提交”时,它应该将这个值与class =“ file_id”值的隐藏值一起使用。

Similary, when I click on submit in second row, it should pickup only those checkbox value in the second row only vice versa. 类似地,当我单击第二行中的“提交”时,它应仅选择第二行中的那些复选框值,反之亦然。

Please check and help me on the code. 请检查并帮助我代码。

Thanks in advance for your time. 在此先感谢您的时间。

Thanks! 谢谢! Robin 罗宾

Use this Code 使用此代码

  • Working DEMO here jsFiddle 在这里工作的演示jsFiddle

     $(function () { $(".assign").click(function(){ var chk = $(this).closest('tr').find('input:checkbox'); var fileid=$(this).closest('tr').find('.file_id').val(); alert("File ID : " +fileid); alert("read :" +chk[0].checked); alert("Write: " +chk[1].checked); alert("Download: "+ chk[2].checked); }); }); 

复选框的值在未选中时设置为null,在选中时设置为“ on”,您需要放置一些逻辑服务器端来使用它,因为如果提交,您将拥有一个类似“ 1”的值:“ on”将值更改为“ read” “写”等并在不为null时使用它

You can use this jQuery code to find out classes of the checked checkboxes corresponding to the specific clicked button and the file_id as well: 您可以使用以下jQuery代码找出与特定单击按钮和file_id对应的选中复选框的类:

$('.assign').click(function(){
   console.log($(this).closest('tr').find('.file_id').val());

    $(this).closest('tr').find('input[type=checkbox]:checked').each(function(){
    console.log($(this).attr('class'));
    });
});

You can use the classes and file_id any way you want. 您可以根据需要使用类和file_id。

Within an event handler this refers to the element the event is triggered on. 在事件处理程序中, this是指触发事件的元素。 Using the current element you can traverse to the highest level of the repeating elements , in this case it would be <tr> then you make all subsequent searches within that main element. 使用当前元素,您可以遍历重复元素的最高级别,在这种情况下,它将是<tr>然后您可以在该主要元素中进行所有后续搜索。

$('.assign').click(function(){
   var $row = $(this).closest('tr');
   var thisRowFileID = $row.find('.file_id').val(); 
   var thisRowCheckedCheckboxs = $row.find(':checkbox:checked');
    /* do something with collection of checked checkboxes*/
});

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

相关问题 如何使用jQuery从表行中的特定单元格中获取复选框值? - How do I get the checkbox value from a specific cell in a table row using jQuery? 如何在jQuery中获取对具有特定值的表行的引用 - How to get a reference to a table row with a specific value in jQuery 使用jquery获取表的特定行的值 - get value of a specific row of a table using jquery 如何在jQuery中获取选中的复选框表值 - How to get checked checkbox table value in jquery (PHP / JQuery?)选中复选框时获取表的行值 - ( PHP /JQuery?) Get the table row value when the checkbox is checked 如果在jQuery中单击该行中的复选框,如何获取行的值 - How to get value of row if checkbox in that row is clicked in jquery 如何使用绝对行号从复选框中使用jQuery获取表单元格的值 - How to get value of table cell with jquery from checkbox using absolute row number 如何使用javascript或jquery获取在表的特定行中存在的复选框和文本框值 - how to get checkbox and textbox value present in a particular row of a table using javascript or jquery 如何使用jQuery获取表中每一行的复选框状态? - How to get checkbox state for each row in table using jQuery? 在jQuery中,如何获取使用复选框选择的表的特定行的值? - In jquery how to get the values of a particular row of a table that is selected using checkbox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM