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

前端 未结 3 2058
臣服心动
臣服心动 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条回答
  •  -上瘾入骨i
    2020-11-29 10:50

    Here's a catch-all solution that will look for an input in the button's containing form. If it exists, it'll set the value, otherwise it will create a hidden input and set its value. This can also be useful if you're not wanting to submit the form immediately.

    $(document).on('click', '[name][value]:button', function(evt){
        var $button = $(evt.currentTarget),
            $input = $button.closest('form').find('input[name="'+$button.attr('name')+'"]');
        if(!$input.length){
            $input = $('', {
                type:'hidden',
                name:$button.attr('name')
            });
            $input.insertAfter($button);
        }
        $input.val($button.val());
    });
    

提交回复
热议问题