【问题标题】:How to get value of a hidden field based on a checkbox state using jquery如何使用jquery根据复选框状态获取隐藏字段的值
【发布时间】:2015-05-27 19:24:21
【问题描述】:

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

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

【问题讨论】:

  • 使用这样的 echo 语句,简直是疯了。
  • 请提供您的渲染代码

标签: javascript php jquery checkbox phalcon


【解决方案1】:

为了获得隐藏的输入,您需要更改此行:

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

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

【讨论】:

  • 天啊..你是男人....它的工作。哇这个社区真棒。非常感谢你..
猜你喜欢
  • 2016-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-13
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 2020-02-29
相关资源
最近更新 更多