0

我有一个包含三个复选框列表的页面,这三个是我想要的动态生成的,并且当用户单击复选框值通过帖子传递时,但我只设法捕获了第一个列表的 Esto 值我做了这样的代码:

         $("body").find(".fcID").click(function(){
//  var v = $(this).val();
    //alert(v);
    var form = jQuery('#form');     
    valor = form.serialize();
    $.ajax({
        type : "POST",
        url:"biblioteca/filtra.php",
        data: valor,

        success: function(data){
            $("#tabelafiltro").html(data);
        }
    });

在 html 中,我用她的表单的 id 放置了一个表单,并在该表单中命名了表单,我有复选框,所以:

     <form name="form" id="form" action="" method="post">

<table>
   <tr>
       <td><input type="checkbox" class="fcID" value="<?php echo $linha['fm-cod-com'] ?>"   name="fcID[]"/></td>
    </tr>
</table>
  <table>
<tr>
<td><input type="checkbox" class="fcID" name="fam[]" value="<?php echo $linha['fm-codigo'] ?>" /></td>
</tr>
</table>

</form>

和PHP:

    $id = $_POST['fcID'];
$fam = $_POST['fam'];

echo(count($fam)) . " + " . count($id);

来人帮帮我?

4

2 回答 2

0

你的代码是正确的,你确定 fam[] 复选框被选中了吗?只有当它们具有属性checked="checked"时,复选框才会序列化。

于 2013-06-11T19:36:49.603 回答
0

不幸的是,“名称”没有被 jQuery 转换为数组。所以而不是这个:

echo $_POST['fcID'][0]; // undefined

你有这个

echo $_POST['fcID[]']; // expected value

我创建了以下内容。它有一些限制,但应该做你想做的。如果您能评价我的回答,我将不胜感激。

var form = jQuery('#form');     
valor = form.formToObj();

// formToObj (c) 2012 Frank Forte.
// Please contact me for a free license to use on personal or business website
// @frankforte or frank @ interactinet .com!
jQuery.fn.formToObj = function()
{
    var obj = {}
    jQuery("input,select,textarea",this).each(function(){
        if(jQuery(this).attr("disabled")){
            return;
        }

        var n = jQuery(this).attr("name") || jQuery(this).attr("id")
        var v = jQuery(this).val();
        // e.g.<input name="test[one][two][three]" value="hello world">

        if(!n.match(/\[/))
        {
            obj[n] = v
        }
        else
        {
            // get keys of array, e.g. "one","two","three"
            var keys = []
            nkeys= n.split('[')
            var i = 0;
            for(k in nkeys)
            {
                if(i > 0)
                {
                    var nk = nkeys[k]
                    if(typeof nk == "string")
                    {
                        if(nk.match(/\]/))
                        {
                            nk = nk.replace("]","")
                        }
                        keys.push(nk);
                    }
                }
                i++
            }
            // name e.g. "test"
            n = n.replace(/^([^\[\]]+)\[.*$/i,"$1");

            // create object and add value then array keys from bottom up
            var iobj = {}
            for(i = keys.length; i > 0; i--)
            {
                j = i-1;
                var k = keys[j]
                if(k==""){k = 0;}

                if(i == keys.length)
                {
                    iobj[k] = v
                }
                else
                {
                    iobj[k] = iobj
                }
            }
            // Need to start with obj[n] and add new values under appropriate keys
            // prevents unsetting or overwriting keys deeper than n
            if(typeof obj[n] == "undefined")
            {
                obj[n] = {}
            }
            obj[n][k] = iobj[k]
        }
    })
    return obj
}
于 2013-06-14T15:11:45.590 回答