【问题标题】:How to get selected values to jQuery?如何将选定的值传递给 jQuery?
【发布时间】:2018-10-31 10:02:04
【问题描述】:
foreach($data as $row)
{
    $output .= '
    <tr>
      <td class="col-xs-3">'.$row->policyno.'</td>
    <td class="col-xs-3">'.$row->q_tree.'</td>
    <td class="col-xs-3">'.$row->Tt_tree.'</td>
    <td class="col-xs-3"> 
      <input id="checkbox" style="margin-left:20px" value='.$row->policyno.' type="checkbox" name="plcdata[]" />
        </td>
    </tr>
    ';
}

我使用此循环查看表中的数据并使用 Ajax 传递到查看页面,并且我添加了一个复选框作为表中选择行的输入。我的问题是如何使用 javascript name="plcdata[] 复选框输入数组获取选定的行?

【问题讨论】:

    标签: javascript php jquery html codeigniter


    【解决方案1】:

    我将获取所有选中的行并将它们存储在一个数组中:

    let values = [];
    $("#checkbox").each(function() {
        if($(this).is(":checked")){
             values.push($(this).parent().parent()); //first parent is td, it parent is tr
        }
    });
    

    【讨论】:

    • 我真的很惊讶,因为问题中没有任何 jQuery 标签,但答案仍然被接受!!!!!!!!!!!!
    【解决方案2】:

    您可以使用以下方法获取所有选中的复选框:

    <input id="UNIQUE_ID" style="margin-left:20px" value='.$row->policyno.' type="checkbox" name="plcdata[]" class="plcdata"/>
    
    var policyno = [];
    var checkedBoxes = $('.plcdata:checked');
        $.each(checkedBoxes, function () {
        policyno.push(value);
    })
    

    您还应该为所有复选框使用唯一 id,如果您想添加 id 否则可以将其删除。

    【讨论】:

      【解决方案3】:
      <div><input type="checkbox" value="4" class="chk"> Value 4</div>
      
      价值 5

      $(document).ready(function(){
          $('#getValue').on('click', function(){
              // Declare a checkbox array
              var chkArray = [];
              
              // Look for all checkboxes that have a specific class and was checked
              $(".chk:checked").each(function() {
                  chkArray.push($(this).val());
              });
              
              // Join the array separated by the comma
              var selected;
              selected = chkArray.join(',') ;
              
              // Check if there are selected checkboxes
              if(selected.length > 0){
                  alert("Selected checkboxes value: " + selected);	
              }else{
                  alert("Please select at least one checkbox.");	
              }
          });
      });
      this was found on another web page. this is work properly. that was solved my problem.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-14
        • 1970-01-01
        • 2011-06-15
        • 2019-12-08
        • 2020-02-11
        • 2022-07-27
        相关资源
        最近更新 更多