douludi8413 2012-05-07 04:31
浏览 17
已采纳

通过ajax到php的数组

After putzing around some, I have gotten this code to work to show me an alert with the proper array of ids that are checked:

alert($("input[name=our_types]:checked").map(function () {return this.value;}).get().join(","));

I need to be able to pass it to the post action function here:

    $.post(action, {
        form_name: $('#form_name').val(),
        site_id: $('#site_id').val()


    },

I've tried doing:

   var our_types = $("input[name=our_types]:checked").map(function () {return this.value;}).get().join(",");

And then pass it like this:

    $.post(action, {
        form_name: $('#form_name').val(),
        site_id: $('#site_id').val(),
                    our_types: $('#our_types').val()

    },

But to no avail. I've been stuck on this for hours, maybe a fresh eye can help.

Heres a working code for another example:

jQuery(document).ready(function(){

    $('#contactform').submit(function(){

        var action = $(this).attr('action');

        $("#message").slideUp(750,function() {
        $('#message').hide();

        $('#submit')
            .after('<img src="assets/ajax-loader.gif" class="loader" />')
            .attr('disabled','disabled');

        $.post(action, {
            USERNAME: $('#USERNAME').val(),
            PASSWORD: $('#PASSWORD').val()
        },
            function(data){
                document.getElementById('message').innerHTML = data;
                $('#message').slideDown('slow');
                $('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
                $('#submit').removeAttr('disabled');
                if(data.match('success') != null) $('#contactform').slideUp('slow');

            }
        );

        });

        return false;

    });

});
  • 写回答

1条回答 默认 最新

  • douchen9569 2012-05-11 01:13
    关注

    You're very close.

    When you

    alert($("input[name=our_types]:checked").map(function () {return this.value;}).get().join(","));
    

    and seeing the content is what you want.

    All you need to do is assign that to a variable:

    var our_types_vals = $("input[name=our_types]:checked").map(function () {return this.value;}).get().join(",");
    

    and pass that variable into the post() function like this:

    $.post(action, {
        form_name: $('#form_name').val(),
        site_id: $('#site_id').val(),
        our_types: our_types_vals
      },  // ... other codes
    );
    

    You can also bypass assigning it to a variable and pass the operation directly inside the property-value pairs object like this:

    $.post(action, {
        form_name: $('#form_name').val(),
        site_id: $('#site_id').val(),
        our_types: $("input[name=our_types]:checked").map(function () {return this.value;}).get().join(",")
      },  // ... other codes
    );
    

    Please pay attention to remove the ending semi-colon on the operation to prevent syntax error inside an object literal.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题