0

I am using jquery ajax to post form contents to the server like this:

$.ajax({
        type: type,
        dataType: "json",
        url: url,
        data: form.serialize(),
        success: onSuccess
});

I have to send an array of data as though it came from a checkbox.

I don't want to create a hidden checkbox on the fly and append() it to the form before submission. I dont see any other methods in the form jquery api that could help me. Any help?

Thanks, Chris.

4

1 回答 1

0

我意识到这篇文章很旧,可能与我的情况不完全相同——但是当我遇到一个非常相似的问题时,它在搜索中出现了,所以就在这里。在我的例子中,服务器需要一种特定的数据格式: $_POST['Category'] = Array([id]=>value)所以我使用嵌套对象来格式化数据,并使用 jQuery 通过 Ajax POST 提交。在此示例中,我们从 DOM 元素中获取一些类别 ID,然后在保存数据时将其用作键,并通过函数派生值:

var items = {};
var fn = 'Category';
var val = {};
items[fn] = val;
var item;
$("#div.data").each(function(){
    item = $(this).val();
    items.Category[item] = myval();
});
function myval() {
    // get data for the value part
    return data;
}
$.ajax({
    type: 'POST',
    cache: false,
    url: window.location.href,
    data: items,
    success: function(data){/* callback */}
});
于 2012-09-29T04:22:44.177 回答