dpikoto468637 2012-07-13 06:40
浏览 49
已采纳

Ajax关联数组

I'm confused about how associative indexing and javascript.

In my javascript, I do this:

$("input[type=button]").click(function () {
    var checked = {};
    $('input[type=checkbox]').each(function() {
        if ($(this).is(':checked')) {
            checked[$(this).attr('name')] = $(this).val();
        }
    });

    var value = [];
    value.push($(this).siblings("input[type=text]").val());

    $.ajax({ url: "scripts/php/generatehtml.php",
        data: {action: value},
        type: 'post',
        dataType: "json",
        success: ...<cut for brevity>});

So in my PHP code, I do this:

if (isset($_POST['action']) && !empty($_POST['action'])) {
    var_dump($_POST['action']);
}

And get this:

array(1) {
[0]=>
  string(5) "12277"
 }

As you can see, that's an indexed array, I want an associative array. But whenver I do something like:

value['id'] = $(this).siblings("input[type=text]").val()

Nothing gets sent.

I'm not very good wtih Ajax, and any help here would be really great!

NOTE: I did look at the other ajax array answers, but I don't think they answer my question.

  • 写回答

3条回答 默认 最新

  • doukao8851 2012-07-13 06:42
    关注

    If you want an associative array, initialize your variable as one. That way you can add values corresponding to specific properties like so:

    var value = {};
    value['id']=$(this).siblings("input[type=text]").val();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大