Ajax post serialize() does not include button name and value

前端 未结 3 2059
臣服心动
臣服心动 2020-11-29 10:10

My $.ajax() is not serializing the button name and value.

I have a very simple form. It has a button and a textbox.

3条回答
  •  攒了一身酷
    2020-11-29 10:38

    I like @slashingweapon 's approach, but why not even shorter, like this?

    $("button.positive").click(function () {
        var result = $(this).parents('form').serialize() 
            + '&' 
            + this.name
            + '='
            + this.value
        ;
        console.log(result);
    
        return false; // prevent default
    });
    

    Only if the server generates non-ascii button names or values, it would be like this:

    $("button.positive").click(function () {
        var result = $(this).parents('form').serialize() 
            + '&' 
            + encodeURI(this.name)
            + '='
            + encodeURI(this.value)
        ;
        console.log(result);
    
        return false; // prevent default
    });
    

提交回复
热议问题