如何使用jquery基于复选框状态获取隐藏字段的值

时间:2022-08-25 21:44:27

i'am working on a small phalcon project where on a particular page it has a search box to search customers, the search results are then displayed under foreach loop having checkboxes for each results along with a hidden field that has a value. The search results are displayed as an ajax response on button click. On checking a particular checkbox, is there anyway to pass the value of that particular hidden field associated with the checked checkbox . Below the search box, there is a form that takes input values from the user along with the customer name value and id passed from the search results. I tried including jQuery script to pass the hidden field value to an input field, iam getting desired output only when the search result is one, but when the search results are more than one, then only the first hidden field value is passed, even if i check second or third checkbox. I'am a biginner in programming. please help. My code is as follows:

我正在开发一个小型phalcon项目,在一个特定页面上它有一个搜索框来搜索客户,然后搜索结果显示在foreach循环下,每个结果都有一个复选框以及一个有值的隐藏字段。搜索结果在按钮单击时显示为ajax响应。在检查特定复选框时,无论如何都要传递与选中复选框关联的特定隐藏字段的值。在搜索框下方,有一个表单,该表单从用户获取输入值以及从搜索结果传递的客户名称值和id。我尝试使用jQuery脚本将隐藏字段值传递给输入字段,仅当搜索结果为1时才获得所需输出,但是当搜索结果多于1时,则只传递第一个隐藏字段值,即使我检查第二个或第三个复选框。我是编程方面的一个重要人物。请帮忙。我的代码如下:

public function editengagementAction($id)
{
    $engmt = Engagement::findFirstById($id);
    $eid=$engmt->customerid;
    $cusdetails= Customerdetails::findFirstById($eid);
    $this->view->objs = $cusdetails;
    if(isset($_POST['searchrslt'])){
        $rst = $_POST['searchrslt'];
        $outputs = $this->db->query("SELECT * FROM customerdetails WHERE name LIKE '%".$rst."%' ")->fetchAll();

        echo"<table width='98%' class='table table-condensed' align='right' id='srchtable' ><thead>";
        echo"<tr><th class='mi_table_head'  style='padding-left:10px;'>User Name</th>";
        echo"<th class='mi_table_head'  style='padding-left:10px;'>Company Details</th>";
        echo"<th class='mi_table_head'  style='padding-left:10px;'>Email</th>";
        echo"<th class='mi_table_head'  style='padding-left:10px;'>Phone NUmber</th>";
        echo"<th class='mi_table_head'  style='padding-left:10px;'></th>";
        echo"<th class='mi_table_head'  style='padding-left:10px;'></th></tr>";
        $count=1;
        foreach ($outputs as $value) {
            echo"<tr><td height='40'  class='mi_table_text'  style='padding-left:10px;'>$value[name]</td>";
            echo"<td valign='middle' class='mi_table_text'  style='padding-left:10px;'>$value[companyname]<br>$value[companyurl]</td>";
            echo"<td valign='middle' class='mi_table_text'  style='padding-left:10px;'>$value[email]</td>";
            echo"<td valign='middle' class='mi_table_text'  style='padding-left:10px;'>$value[phonenumber]</td>";
            echo"<td valign='middle' class='mi_table_text'  style='padding-left:10px;'><input type='checkbox' name='checkbox'  id='count' value='$value[name]'></td>";
            echo"<td valign='middle' class='mi_table_text'  style='padding-left:10px;'><input type='hidden' id='$count' name='cid' value='$value[id]'></td></tr>";
            echo "<script>
            $(function(){ $('#addsubmit').on('click',function(){
             $('#checkbox').removeAttr('checked');
             var boxChecked = false;
             var customid;
             $('input[type=\"checkbox\"]').each(function() {
              if ($(this).is(':checked')) {
             boxChecked = $(this).val();
             customid=$('input[type=\"hidden\"]').val();
               alert('id is'+ customid);
               }
                });
               $('#custname').val(boxChecked);
               $('#custmid').val(cusid);
             if (boxChecked == false) {
             alert('Please select one customer');
             return false; }
             alert('Customer Changed !');
             return false;
             }); });
             </script>";
            $count++;
        }
        echo"<tr><td colspan='10' align='right'><input type='button' value='Add Customer' id='addsubmit' class='button blue' name='button' onclick='addcustmr()'></td></tr>";
        echo"</tbody><tbody><tbody><tbody></thead></table>";
        exit();
        $this->view->disable();
    }  
  }

如何使用jquery基于复选框状态获取隐藏字段的值

1 个解决方案

#1


2  

In order to get to the hidden input you need to change this line:

要获取隐藏的输入,您需要更改此行:

customid=$('input[type=\"hidden\"]').val();

To

customid=$(this).parent().next().children('input[type=\"hidden\"]').val();

#1


2  

In order to get to the hidden input you need to change this line:

要获取隐藏的输入,您需要更改此行:

customid=$('input[type=\"hidden\"]').val();

To

customid=$(this).parent().next().children('input[type=\"hidden\"]').val();