提交带有额外id的jQuery表单序列化 [英] jQuery form serialization submitted with an extra id

查看:134
本文介绍了提交带有额外id的jQuery表单序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的表格:

 <form id="cform">
    <label>folder name:</label>
    <input type="text" name="Folder[name]" />
    <input id="cfolder" type="submit" value="create" />
 </form>

我想在jQuery ajax中提交表单,所以我有这样的代码:

I want to submit the form in jQuery ajax, so I have code like this:

 $.ajax({
    type: "POST",
    url: "/folder/create",
    data: $('#cform').serialize(),
    success: function(msg){
        //sucess
    }
  });

到目前为止这很容易,但现在我想提交一个额外的动态ID($ id值)可用)以及整个表格。我怎样才能做到这一点?我试过这个:

this is all easy so far, but now I want to submit an extra dynamic id($id value is available) along with the whole form. how Can I do that? I tried this:

 data: $('#cform').serialize()+"&Folder[id]="+$id,

但它不起作用,服务器端没有获得id。这里的棘手部分是Folder [name],Folder [id],
,因为服务器端接收php中的数据,如下所示:

but it didn't work, the server side did not get the id. the tricky part here for me is Folder[name], Folder[id], because the server side recevie the data in php like this:

if(isset($_POST['Folder'])){
    //assign data here
}

希望这是明确的,并感谢任何建议。

hope this is clear and thanks for any suggestion.

推荐答案

As另一种建议:

您可以使用 .serializeArray [docs] $。param [docs] 。然后jQuery将处理所有编码:

You can use .serializeArray [docs] and $.param [docs]. Then jQuery will take care of all the encoding:

var data = $('#cform').serializeArray();
data.push({name: "Folder[id]", value: $id});

// later

data: $.param(data);






我不明白你是否有问题使用 $ _ POST ['Folder'] ,但如果你在末尾使用带括号的名称,PHP会将数据解析为数组。


I don't understand if you have a problem with $_POST['Folder'] or not, but if you use names with brackets at the end, PHP will parse the data into an array.

因此,您可以使用 $ _ POST ['Folder'] ['name'] $ _ POST访问数据['Folder'] ['id']

有关更多信息,请查看 来自外部来源的变量

For more information have a look at Variables From External Sources.

这篇关于提交带有额外id的jQuery表单序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆