简体   繁体   English

复选框-如果选中了连续的复选框,如何获取连续的数据?

[英]Check-box - how to get the data in a row if the checkbox in a row is checked?

I want to make a list of medicines and check all the medicines that I want to buy. 我要列出药品清单并检查我要购买的所有药品。 I've thought of using a checkbox to check all the medicines that I wish to order then transfer the name of the medicine to the Javascript once the order button is clicked. 我曾考虑过使用一个复选框来检查我希望订购的所有药物,然后在单击订购按钮后将药物名称转移到Javascript。

I'm quite new to Javascript and jQuery so I'm not sure how to make this work. 我是Java和jQuery的新手,所以不确定如何使它工作。 I've searched other codes but I can't find any that can be related to my problem. 我搜索了其他代码,但找不到与我的问题有关的任何代码。

Here is the table code I made: 这是我制作的表代码:

 <table id="example1" >
    <thead>
        <tr>
            <th></th>
            <th>Brand Name</th>
            <th>Medicine Name </th>
            <th>Price</th>


        </tr>
    </thead>
    <tbody>


        <tr>
            <td style="width: 5px"><input type="checkbox" /></td>                                               
            <td>Xeloda</td>
            <td> Capecitabine</td>
            <td>$150 </td>
        </tr>
        <tr>
            <td style="width: 5px"><input type="checkbox" /></td>                                               
            <td>Camptosar</td>
            <td>Irinotecan Hydrochloride</td>
            <td>$200 </td>
        </tr>
        <tr>
            <td style="width: 5px"><input type="checkbox" /></td>                                               
            <td>Eloxatin</td>
            <td>Oxaliplatin</td>
            <td>$150 </td>
        </tr>




</table>
<input type="button" value="order" onclick="getAllMedicineNameOfCheckedBox()"/>
$(":checked").closest("tr").children("td").get()

will return an array of all the td elements in a row. 将返回一行中所有td元素的数组。 You will want to do some additional code, to handle multiple checkboxes, etc and maybe tie it to click handlers. 您将需要执行一些其他代码,以处理多个复选框,等等,并可能将其绑定到单击处理程序。

$("input:checkbox").on("click", function(){
  if ($(this).is(":checked")){
      var data = $(this).closest("tr").children("td").get();

      /*  loop over TD items.  */
  } 
});

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

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