简体   繁体   English

jQuery:serializeArray和附加值

[英]jQuery: serializeArray and additional value

I have the following page where a form is posted using ajax: 我有以下页面,其中使用ajax发布表单:

<form name="postdata">
    <input type="hidden" name="id" value="XXXX">
    <input type="hidden" name="action" value="add">
    <button type="submit" class="btn btn-success submit">Submit</button>
</form>

<script>
$(function()
{
    $('form[name="postdata"]').on('submit', function()
    {
        var data = $(this).serializeArray()
        $.post('api/Update.php', data, function(r,s)
        {
            console.log(r)
        });
    })
})
</script>

I have hundreds of forms like the one above on my page and It has became preferable to remove the redundand "action" parameter from the form action and add it to the JS function instead. 我的页面上有上百种表单,而从表单操作中删除冗余的“ action”参数,然后将其添加到JS函数中,已成为可取的选择。

It should be something like this: 应该是这样的:

var data = $(this).serializeArray() + "&action=add"

That turns out as: 结果是:

[object Object],[object Object]&action=add

Doesn't work but should be close, I'm just a total JS noob. 不起作用,但应该接近,我只是一个JS新手。 Please help me. 请帮我。

Many thanks! 非常感谢!

Thats because you are concatenating an object( an Array object ) with a string, either use serialize method which returns a string or push an object into the array. 那是因为您正在将一个对象( 一个Array对象 )与一个字符串连接在一起,所以请使用返回字符串的serialize方法或将一个对象压入该数组中。

data.push({
    name: 'action',
    value: 'add'
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM